使用php替换xml中的特定节点值

时间:2013-12-12 10:13:54

标签: php xml arrays xml-parsing

嗨我正在解析多个xml feed并合并成一个并且它对我来说工作正常但是现在我有点卡在一点因为我需要在特定节点值中添加一些前缀我的意思是我需要更改值那个节点。在这里我提供一些示例代码我想要的实际内容。

XML

<JobRecords>
    <JobRecord>
        <Brand>Corporate1</Brand>
        <JobId>45982</JobId>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent1</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
    <JobRecord>
        <Brand>Corporate2</Brand>
        <JobId>45983</JobId>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent2</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
    <JobRecord>
        <Brand>Corporate3</Brand>
        <JobId>45984</JobId>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent3</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>    

在上面的xml中我想添加像<JobId>0-45984</JobId>

这样的前缀

这是 php代码,它将不同的xml feed组合成一个如上所述的put out。

<?php 
    $feed1 = "data1.xml";
    $feed2 = "data2.xml"; 
    $feed3 = "data3.xml";

    $xml1 = new DOMDocument('1.0', 'UTF-8');
    $xml1->load($feed1);

    $xml2 = new DOMDocument('1.0', 'UTF-8');
    $xml2->load($feed2);

    $xml3 = new DOMDocument('1.0', 'UTF-8');
    $xml3->load($feed3);

    $addXml = array();
    $addXml[] = $xml1->saveXML();
    $addXml[] = $xml2->saveXML();
    $addXml[] = $xml3->saveXML();

    // create a new document
    $dom = new DOMDocument();
    $dom->appendChild($dom->createElement('JobRecords'));

    foreach ($addXml as $xml) {
      $addDom = new DOMDocument();
      $addDom->loadXml($xml);
      if ($addDom->documentElement) {
        foreach ($addDom->documentElement->childNodes as $node) {
          $dom->documentElement->appendChild(
            $dom->importNode($node, TRUE)
          );
        }
      }
    }

    $xmlFinal = $dom->saveXml();
    echo $xmlFinal;
?>  

到目前为止我已经尝试但没有成功请告诉我如何实现这一目标 在此先感谢,非常感谢。

2 个答案:

答案 0 :(得分:5)

您可以从root进行迭代,然后检查并替换以下节点。这里$xmlData包含xml的字符串。

$dom = new DOMDocument();
$dom->loadXML($xmlData);
foreach ($dom->documentElement->childNodes as $node) {
//print_r($node);
if($node->nodeType==1){
   $OldJobId = $node->getElementsByTagName('JobId')->Item(0);
   $newelement = $dom->createElement('JobId','0-'.$OldJobId->nodeValue); 
    $OldJobId->parentNode->replaceChild($newelement, $OldJobId);
 }
}
$str = $dom->saveXML($dom->documentElement);
echo $str;

您可以找到有效的演示here

答案 1 :(得分:1)

我不确定为什么你不直接在循环中加载xml feed,就像在previous answer的第二个例子中一样。

将'JobRecord'元素导入目标文档后,你必须找到它的JobId子项并修改它的nodeValue属性

$files= array(
  'xml1.xml',
  'xml2.xml',
  'xml3.xml'
  // ... more xmls
);

$dom = new DOMDocument();
$dom->appendChild($dom->createElement('JobRecords'));
// get an xpath object
$xpath = new DOMXpath($dom);

// $index variable for later use
foreach ($files as $index => $filename) {
  $addDom = new DOMDocument();
  $addDom->load($filename);
  if ($addDom->documentElement) {
    foreach ($addDom->documentElement->childNodes as $node) {
      $dom->documentElement->appendChild(
        $record = $dom->importNode($node, TRUE)
      );
      // find the jobId child elements (should be only one)
      $jobIds = $xpath->evaluate('./JobId', $record);
      foreach ($jobIds as $jobId) {
        // prefix the value with the index
        $jobId->nodeValue = $index.'-'.$jobId->nodeValue;
      }
    }
  }
}

echo $dom->saveXml();