使用PHP拆分XML文件,不输出顶级父节点
我试图将一个XML文件分为两个文件,longrentals.XML和shortrentals.XML,但遇到了最后一个障碍。以下是我希望发生的事情:使用PHP拆分XML文件,不输出顶级父节点,php,xml,domdocument,Php,Xml,Domdocument,我试图将一个XML文件分为两个文件,longrentals.XML和shortrentals.XML,但遇到了最后一个障碍。以下是我希望发生的事情: 解析rentals.xml,对于term=“short”的每个实例,该条目的顶级父级“属性”节点保存到shortrentals.xml 每个实例都将从rentals.xml文件中删除(提取后) 将保存shortrentals.xml文件 原始文件中的其余条目将保存到longrentals.xml XML结构如下所示: <property&
- 解析rentals.xml,对于term=“short”的每个实例,该条目的顶级父级“属性”节点保存到shortrentals.xml李>
- 每个实例都将从rentals.xml文件中删除(提取后)
- 将保存shortrentals.xml文件
- 原始文件中的其余条目将保存到longrentals.xml
<property>
...
<rent>
<term>short</term>
<freq>week</freq>
<price_peak>5845</price_peak>
<price_high>5845</price_high>
<price_medium>4270</price_medium>
<price_low>3150</price_low>
</rent>
...
</property>
$destination = new DOMDocument;
$destination->preserveWhiteSpace = true;
$destination->loadXML('<?xml version="1.0" encoding="utf-8"?><root></root>');
$source = new DOMDocument;
$source->load('file/rentals.xml');
$xp = new DOMXPath($source);
$destRoot = $destination->getElementsByTagName("root")->item(0);
foreach ($xp->query('/root/property/rent[term = "short"]') as $item) {
$newItem = $destination->importNode($item, true);
$destRoot->appendChild($newItem);
$item->parentNode->removeChild($item);
}
$source->save("file/longrentals.xml");
$destination->formatOutput = true;
$destination->save("file/shortrentals.xml");
...
短的
周
5845
5845
4270
3150
...
我使用的代码如下所示:
<property>
...
<rent>
<term>short</term>
<freq>week</freq>
<price_peak>5845</price_peak>
<price_high>5845</price_high>
<price_medium>4270</price_medium>
<price_low>3150</price_low>
</rent>
...
</property>
$destination = new DOMDocument;
$destination->preserveWhiteSpace = true;
$destination->loadXML('<?xml version="1.0" encoding="utf-8"?><root></root>');
$source = new DOMDocument;
$source->load('file/rentals.xml');
$xp = new DOMXPath($source);
$destRoot = $destination->getElementsByTagName("root")->item(0);
foreach ($xp->query('/root/property/rent[term = "short"]') as $item) {
$newItem = $destination->importNode($item, true);
$destRoot->appendChild($newItem);
$item->parentNode->removeChild($item);
}
$source->save("file/longrentals.xml");
$destination->formatOutput = true;
$destination->save("file/shortrentals.xml");
$destination=新文档;
$destination->preserveewhitespace=true;
$destination->loadXML(“”);
$source=新文档;
$source->load('file/rentals.xml');
$xp=新的DOMXPath($source);
$destRoot=$destination->getElementsByTagName(“根”)->项(0);
foreach($xp->query('/root/property/rent[term=“short”]')作为$item){
$newItem=$destination->importNode($item,true);
$desroot->appendChild($newItem);
$item->parentNode->removeChild($item);
}
$source->save(“file/longrentals.xml”);
$destination->formatOutput=true;
$destination->save(“file/shortrentals.xml”);
除了shortrentals.xml中的输出仅包含租金节点,而不包含顶级父属性节点外,此操作也有效。此外,从longrentals.xml中删除的条目仅删除Rent子节点。那么,我如何使用我的代码提升一个级别呢?您可以使用DOMNode的
parentNode
属性来提升结构中的一个级别(类似于在removeChild
代码中的方式)
可选地,考虑专用XML转换语言,来创建这两种XML文件,而不是<代码> Frace< /Cord>循环。在这里,XSLT作为字符串嵌入,但可以像任何其他XML文件一样从文件中解析。假定的XML结构:
…
shortrentals.xml输出
// Load XML and XSL sources
$xml = new DOMDocument;
$xml->load('file/rentals.xml');
$xslstr = '<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="property[rent/term=\'short\']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="property">
<xsl:copy>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>';
$xsl = new DOMDocument;
$xsl->loadXML($xslstr);
// Configure transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// Transform XML source
$newXML = new DOMDocument;
$newXML = $proc->transformToXML($xml);
// Output file
file_put_contents('file/shortrentals.xml', $newXML);
//加载XML和XSL源
$xml=新文档;
$xml->load('file/rentals.xml');
$xslstr='1
';
$xsl=新文档;
$xsl->loadXML($xslstr);
//配置变压器
$proc=新的XSLTProcessor;
$proc->importStyleSheet($xsl);
//转换XML源
$newXML=新文档;
$newXML=$proc->transformToXML($xml);
//输出文件
文件内容('file/shortrentals.xml',$newXML);
longrentals.xml(使用空模板删除节点)
//加载XML和XSL源
$xml=新文档;
$xml->load('file/rentals.xml');
$xslstr='1
';
$xsl=新文档;
$xsl->loadXML($xslstr);
//配置变压器
$proc=新的XSLTProcessor;
$proc->importStyleSheet($xsl);
//转换XML源
$newXML=新文档;
$newXML=$proc->transformToXML($xml);
//输出文件
文件内容('file/longrentals.xml',$newXML);
Doh!肯定是“看不到(XML)树的情况”。非常感谢奈杰尔:)。