Php 使用DOM文档将数据附加到xml文件
我试图将新数据附加到现有的xml文件中。这就是我到目前为止所做的:Php 使用DOM文档将数据附加到xml文件,php,xml,Php,Xml,我试图将新数据附加到现有的xml文件中。这就是我到目前为止所做的: $doc = new DOMDocument(); $doc->formatOutput = true; $doc->loadXml('library.xml'); $path = new DOMXPath($doc); $b=$doc->createElement('book'); $ISBN = $doc->createElement( 'isbn' ); $ISBN->a
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadXml('library.xml');
$path = new DOMXPath($doc);
$b=$doc->createElement('book');
$ISBN = $doc->createElement( 'isbn' );
$ISBN->appendChild(
$doc->createTextNode( $_GET['isbn'] )
);
$b->appendChild( $ISBN );
$edition = $doc->createElement( 'edition' );
$edition->appendChild(
$doc->createTextNode( $_GET['ed'] )
);
$b->appendChild( $edition );*/
$doc->save("library.xml");
我很难找到插入数据的位置。我看到一些人使用query()或:
但是我想知道是否有人可以解释路径是如何编写的。/record[./title[包含(,“%s”)]]
是xpath查询,它意味着在xml中查找包含标题属性的记录节点搜索字符串 您需要获取对所需节点的引用,然后将子节点附加到该节点。您似乎没有将任何位置附加到现有节点 在这里看一看如何获得要附加的位置
谢谢您的帮助,我知道我的代码还没有引用节点。你给我的链接,我在看这一行:$query=//book/chapter/para/informaltable/tgroup/tbody/row/entry[.=“en”];我现在明白了它是从根节点到子节点的,但我有点搞不清楚“entry[.=“en”]”是什么意思。@user977151 xpath中的
entry[.=“en”]
部分表示查找名为“entry”且内容为“en”的节点:en
$query = sprintf('//record[./title[contains(., "%s")]]', $searchString);