问题
Hi i am parsing multiple xml feed and combine into one and its working fine for me but now i am bit of stuck at one point because i need to add some prefix into specific node value i mean i need to change the value of that node. here i am providing some example code what actual i want.
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>
In the above xml i want to append the prefix like this <JobId>0-45984</JobId>
Here is the php code which combine different xml feed into one give out put as above.
<?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;
?>
I have tried so far but didn't succeed Please advise me how to achieve this.
Thanks in advance and much appreciated.
回答1:
You could iterate from root and check and replace the node like following. Here $xmlData holds the string of your 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;
You can find working demo here
回答2:
I am not sure why you don't load the xml feed directly inside the loop, like in the second example of the previous answer.
After importing the 'JobRecord' element into the target, document, you have to find it JobId child and modify it's nodeValue property
$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();
来源:https://stackoverflow.com/questions/20540592/replace-specific-node-value-in-xml-using-php