在XML文件中使用php删除开始标记和结束标记之间的所有内容的最佳方法是什么?
我有一个像这样的XML文件
<CATALOG id="1">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="2">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="3">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
例如,我想删除第一个<CATALOG id="1"></CATALOG>
有什么建议吗?
答案 0 :(得分:1)
类似这样的事情
//修改
请注意,如果只清除内容,此解决方案将删除该元素,然后使用jazZRo中的解决方案。
$xml = '<data><CATALOG id="1">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="2">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="3">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG></data>';
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
foreach($xpath->query("//CATALOG[@id=3]") as $node )
{
$node->parentNode->removeChild($node);
}
echo $doc->saveXML();
答案 1 :(得分:1)
DOMNode :: nodeValue是一个可写属性。由于实体处理失败,我不会用它来设置值,但是空字符串不包含任何实体。
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
foreach($xpath->evaluate("//CATALOG[@id=3]") as $node) {
$node->nodeValue = '';
}
echo $doc->saveXML();
答案 2 :(得分:0)
可以使用DOMDocument和DOMXPath完成。例如:
$dom = new DOMDocument();
$dom->loadXML($xml); // loads XML from a string, use $dom->load() to load from a file
$xpath = new DOMXPath($dom);
$cat = $xpath->query('//CATALOG[@id="1"]')->item(0);
while($cat->firstChild) {
$cat->removeChild($cat->firstChild);
}
答案 3 :(得分:-1)
您可以使用DOMDocument
类使用以下解决方案:
//the xml content.
$xml = '
<CATALOGS>
<CATALOG id="1">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="2">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
<CATALOG id="3">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
</CATALOGS>';
//load the XML string as DOM document.
$domDoc = new DOMDocument();
$domDoc->loadXML($xml);
//create XPath object to navigate through the XML content.
$xpath = new DOMXPath($domDoc);
//find all elements "CATALOG" with the attribute "id" with value "1"
//and run through the found elements.
foreach ($xpath->query("//CATALOG[@id=1]") as $domNode) {
$domNode->nodeValue = '';
}
$xml = $domDoc->saveXML();
注意:您的XML字符串格式不正确。缺少XML的根元素。为了提供可行的解决方案,我添加了
<CATALOGS>
根元素。