好吧,我有点难过。在操作XML时我对Python比较熟悉,但如果可能的话,我想在这个实例中使用PHP。
是否有一种相对简单的方法来替换一个文本块 - 给定节点的所有子节点和值 - 在另一组元素的XML文件中?
从这开始:
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:myns="http://www.nsdl.org/ontologies/relationships#">
<rdf:Description rdf:about="info:fedora/cfai:EB01a004">
<fedora:isMemberOfCollection rdf:resource="info:fedora/cfai:collection"/>
</rdf:Description>
</rdf:RDF>
我希望它看起来像这样:
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:myns="http://www.nsdl.org/ontologies/relationships#">
<rdf:Description rdf:about="info:fedora/cfai:EB01a004">
<element>These are new elements that I paste in.</element>
<element>This is another, basically just a string of elements</element>
</rdf:Description>
</rdf:RDF>
我已经加载了 simple_xml_loadfile ,我已经使用 registerXPathNamespace 进行了命名空间的烹饪,但是我有一个非常困难的时间来弄清楚如何1删除节点,2)在我们的位置插入我自己的文本块。
非常感谢任何帮助。
答案 0 :(得分:0)
您可以使用unset()删除节点:
unset($xml->node);
然后添加一个元素:
$node = $xml->addChild('node');
$node->addChild('title', 'i'm a node');
$node->addChild('title2', 'another node');
答案 1 :(得分:0)
结束序列化XML,然后使用正则表达式查找并替换元素内的所有内容。这是一种奇怪的方式,但很适合这种情况。感谢大家提出的建议,帮助推动事情发展。