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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么