0
嗨我通过一个XML文档循环搜索特定的Word(那工程),然后我想要一个特定的元素,如果该单词包含在该部分。如何查找某个XML元素并将其添加为新的?
<product>
<title>TestProduct</title>
<Specifications>
<item name="Specifications1">Test</item>
<item name="Specifications2">Hello World</item>
</Specifications>
<body>
<item name="Color">Black</item>
</body>
</product>
我尝试这样做:
<?php
$dom = new DOMDocument;
$dom->load('Test.xml');
# set up the xpath
$xpath = new DOMXPath($dom);
$append= $dom->getElementsByTagName('Text');
print_r($append);
foreach ($xpath->query("*[contains(., 'Test')]") as $item) {
$element = $dom->createElement('ID', '123');
$append->appendChild($element);
}
?>
,但它不工作,能有人给我一个提示?坦克;)
所以我应该在循环之后添加的元素? – NeunNatter
其实它不会添加任何我不知道该怎么做 – NeunNatter
你有一些XML来测试这个吗?它使它更容易。 –