Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在简单XML文件上使用DOMDocument PHP类_Php_Xml_Domdocument - Fatal编程技术网

在简单XML文件上使用DOMDocument PHP类

在简单XML文件上使用DOMDocument PHP类,php,xml,domdocument,Php,Xml,Domdocument,我的XML文档如下所示 <?xml version="1.0" encoding="utf-8"?> <userSettings> <tables> <table id="supertable"> <userTabLayout id="A">{"id":"colidgoeshereX"}</userTabLayout> <userTabLayout

我的XML文档如下所示

<?xml version="1.0" encoding="utf-8"?>
<userSettings>
    <tables>
        <table id="supertable">
            <userTabLayout id="A">{"id":"colidgoeshereX"}</userTabLayout>
            <userTabLayout id="B">{"id":"colidgoeshereY"}</userTabLayout>
        </table>
        <table id="almost-supertable">
            <userTabLayout id="A">{"id":"colidgoeshereC"}</userTabLayout>
            <userTabLayout id="B">{"id":"colidgoeshereD"}</userTabLayout>
        </table>
    </tables>
</userSettings>
$y
变量现在包含一个
domeElement
对象,其
nodeValue
属性包含如下字符串:

["nodeValue"]=>
string(81) "
        {"id":"colidgoeshereX"}
        {"id":"colidgoeshereY"}
"
我的问题是,
节点发生了什么?为什么我不将其视为
节点的子节点?如果我想访问
节点,我该怎么做


通常我会阅读关于这类内容的文档,但是官方PHP页面上的官方文档非常稀少。

好吧,您可以不使用DomXpath

$xmlS='1
{“id”:“colidgoeshereX”}
{“id”:“colidgoeshereY”}
{“id”:“colidgoeshereC”}
{“id”:“colidgoeshereD”}
';
$xmldoc=新的DOMDocument();
$xmldoc->loadXml($xmlS);
$table1=$xmldoc->getElementsByTagName(“表”)->项(0);
$userTabLayoutA=$table1->getElementsByTagName(“userTabLayout”)->项(0);
$userTabLayoutB=$table1->getElementsByTagName(“userTabLayout”)->项(1);
echo$userTabLayoutA->nodeValue;//{“id”:“colidgoeshereX”}
echo$userTabLayoutB->nodeValue;//{“id”:“colidgoeshereY”}
如您所见,您可以使用
getElementsByTagName
并指定所需的项目,逐个访问所有元素

我的问题是,
节点发生了什么

没发生什么事。
元素是
$y
中的doElement的子元素

为什么我不将其视为
节点的子节点

因为您没有使用
nodeValue
字段查找子节点

如果我想访问
节点,我该怎么做

通过遍历文档,例如通过
childNodes
字段访问子节点(惊喜)

通常我会阅读关于这类内容的文档,但是官方PHP页面上的官方文档非常稀少

这是因为W3C已经记录了DOM,例如:

因为该链接是规范,它可能有点技术性,所以更难工作,但是你可以考虑它完成(例如,如果你需要知道它,检查真实性)。一旦理解了指定的DOM,您就可以关联任何可用的DOM文档,例如在MDN中,甚至在Javascript(W3C的两个默认实现之一)中,它在PHP中的工作方式类似(特别是遍历或Xpath的工作方式)


祝你阅读愉快

很好。我想我只是对这个库缺乏好的文档感到失望。它显然是一个非常强大的XML导航引擎,但是缺少文档。是的,当你不得不按照自己的方式去做时,当你可以沿着一条非常跟踪和干净的路径前进时,这是非常令人沮丧的。是的。如果你知道FM在哪里,RTFM总是一个不错的选择。下面是另一个提示:检查SO中的标签,然后查看or的答案。他们通常会很详细地回答DOM和PHP知识。这是从XMLDOM提取数据的简单方法。它是W3C标准,这就是为什么它不是PHP手册的一部分。但是使用
DOMXpath::evaluate()
query()
只支持返回节点列表的表达式。现代浏览器(IE除外)中的
HTMLDocument::evaluate()
也使用相同的表达式。
["nodeValue"]=>
string(81) "
        {"id":"colidgoeshereX"}
        {"id":"colidgoeshereY"}
"
$xmlS = '
<userSettings>
    <tables>
        <table id="supertable">
            <userTabLayout id="A">{"id":"colidgoeshereX"}</userTabLayout>
            <userTabLayout id="B">{"id":"colidgoeshereY"}</userTabLayout>
        </table>
        <table id="almost-supertable">
            <userTabLayout id="A">{"id":"colidgoeshereC"}</userTabLayout>
            <userTabLayout id="B">{"id":"colidgoeshereD"}</userTabLayout>
        </table>
    </tables>
</userSettings>
';

$xmldoc = new DOMDocument();
$xmldoc->loadXml($xmlS);

$table1 = $xmldoc->getElementsByTagName("tables")->item(0);
$userTabLayoutA = $table1->getElementsByTagName("userTabLayout")->item(0);
$userTabLayoutB = $table1->getElementsByTagName("userTabLayout")->item(1);

echo $userTabLayoutA->nodeValue; // {"id":"colidgoeshereX"}
echo $userTabLayoutB->nodeValue; // {"id":"colidgoeshereY"}