SimpleXML、关联数组和 XPath

发布于 2024-07-27 12:51:36 字数 612 浏览 5 评论 0原文

我有一个关于 xpath 和数组的问题。 我想知道是否可以在某些 simpleXML 上使用 xpath 并让它返回节点名称及其值的关联数组。 例如,假设我有以下 XML:

<element1 page="1">blah</element1>
<element2 page="1">blah blah</element2>
<element3 page="2">blah</element3>
<element4 page="3">blah blah</element4>

现在,如果我要执行 $xml->xpath('//node()[@page="1"]'); 那么它会返回一个如下所示的数组:

array( 0 => 'blah' , 1 => 'blah blah' );

是否可以得到一个类似于下面的数组?

array( element1 => 'blah' , element2 => 'blah blah' );

谢谢您的帮助!

I have a question about xpath and arrays. What I was wondering was if it is possible to use xpath on some simpleXML and have it return an associative array of node names and their values. For example, say I have the following XML:

<element1 page="1">blah</element1>
<element2 page="1">blah blah</element2>
<element3 page="2">blah</element3>
<element4 page="3">blah blah</element4>

Now if I were to go $xml->xpath('//node()[@page="1"]'); then it would return an array like the following:

array( 0 => 'blah' , 1 => 'blah blah' );

Is it possible to get an array similar to the one below?

array( element1 => 'blah' , element2 => 'blah blah' );

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-08-03 12:51:36

我不认为你可以将其获取到那种数组中(你需要告诉 PHP 放什么标签、子节点、属性等),但你可以使用 DOMXPath 类,它为您提供一个 DOMNodeList 对象:

$document = new DOMDocument();
$document->load($myXmlFile);
$xpath = new DOMXPath($document);

$result = $xpath->query('//node()[@page="1"]');
var_dump($result->length); // int(2)
var_dump($result->item(0)->tagName); // string(8) "element1"
var_dump($result->item(1)->tagName); // string(8) "element2"

I don't think you can fetch that into that kind of array (you would need to tell PHP what tags, child nodes, attributes, etc to put there), but you can fetch DOMNode elements using the DOMXPath class, which gives you a DOMNodeList object:

$document = new DOMDocument();
$document->load($myXmlFile);
$xpath = new DOMXPath($document);

$result = $xpath->query('//node()[@page="1"]');
var_dump($result->length); // int(2)
var_dump($result->item(0)->tagName); // string(8) "element1"
var_dump($result->item(1)->tagName); // string(8) "element2"
温柔女人霸气范 2024-08-03 12:51:36

对于简单的 XML,数组并不完全像您提到的那样,它更像是:

$result = array( 0 => simplexmlObject('blah') , 1 => simplexmlObject('blah blah') );

因为您有一个 SimpleXML 对象而不是文字字符串,您仍然可以访问完整的 SimpleXML 文档:

$result[0]->addChild("another", "child");

// which is <element1 page="1">blah<another>child</another></element1>

或者更接近您的问题

$name = $result[0]->getName();

,如果您对所有事情都着迷你可以使用 DOM 属性,然后你可以执行以下操作:

$parent = dom_import_simplexml($result[0])->parent; 

For simple XML the array is not exactly as you mentioned it's more like:

$result = array( 0 => simplexmlObject('blah') , 1 => simplexmlObject('blah blah') );

because you have a SimpleXML object and not a literal string you still have access to the full SimpleXML document:

$result[0]->addChild("another", "child");

// which is <element1 page="1">blah<another>child</another></element1>

or closer to your question

$name = $result[0]->getName();

and if you are smitten with all the things you can do with DOM properties then you can do something like:

$parent = dom_import_simplexml($result[0])->parent; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文