doulv1760 2016-03-10 21:14
浏览 42

XML ID通过节点ID而不是名称替换xml文件中的数据

This is a partial of my xml file now

<entrys>
   <customer>
    <name> foo </name>
    <city> bar </city>
   </customer>
</entrys>

This is what I'll change it to...

<entrys>
   <customer>
    <name id="1"> foo </name>
    <city id=”2”> bar </city>
   </customer>
</entrys>

The variable I have reflects the first example...

<?php

    if (isset($_POST['lsr-submit']))
      {
        header('Location: http://www.domain.net/test.php');
      }

$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];

$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);

$xml->reports = "";
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml');

?>

here is part of my form...

<form name="lsrReports" action="xml/process.php" onSubmit="return defaultagree(this)" method="post" >
<table width="50%" align="center" cellpadding="2" cellspacing="0">
<tr>
<td> name:</td><td> <input type="text" name="name" value=<?php echo $xml->customer->name; ?> /></td>
</tr>
<tr>
<td> city:</td><td> <input type="text" name="city" value=<?php echo $xml->customer->city; ?> /></td>
</tr>
<tr>
<td> state:</td><td> <input type="text" name="state" value=<?php echo $xml->customer->state; ?> /></td>
</tr>
</form>

My question is how can I do this operation by id instead of name using what I've written so far? I hoping it's something simple :/

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题