简体   繁体   English

使用php从xml文件中获取嵌套标签值

[英]get nested tag value from xml file using php

i have an xml file in a special format how can i get specific tag value out of it for example if i want to get rotation value of each dict tag. 我有特殊格式的xml文件,例如,如果我想获取每个dict标签的旋转值,如何从中获取特定的标签值。 as you can see that rotation value of each dict is nested in array tag that is nested in dist tag.please point me in right direction 如您所见,每个字典的旋转值嵌套在嵌套在dist标签中的array标签中。请指向正确的方向

<?xml version="1.0" encoding="UTF-8"?>
<pu version="1.0">
  <dict>
    <key>ID</key>
    <string>C0AC8773-CEE6-4A12-9C69-320A1BDB7255</string>
    <key>Items</key>
    <array>
      <dict>
        <key>opacity</key>
        <real>1</real>
        <key>Thickness</key>
        <real>0</real>
        <key>repeat</key>
        <false/>
        <key>rotation</key>
        <real>90</real>
      </dict>
      <dict>
        <key>opacity</key>
        <real>1</real>
        <key>Thickness</key>
        <real>0</real>
        <key>repeat</key>
        <false/>
        <key>rotation</key>
        <real>180</real>
      </dict>
      <dict>
        <key>opacity</key>
        <real>1</real>
        <key>Thickness</key>
        <real>0</real>
        <key>repeat</key>
        <false/>
        <key>rotation</key>
        <real>270</real>
      </dict>
    </array>
  </dict>
</pu>

this is what i tried so far 这是我到目前为止尝试过的

$dom = new DOMDocument;
$dom->load($path);
$array = $dom->getElementsByTagName('array');
foreach($array as $key)
{
   print_r($key);
}

this will print all tags inside array tags but i want only rotation value 这将打印数组标签内的所有标签,但我只需要旋转值

Paste those XML data on a file say yourxmlfile.xml and use simplexml_load_file() to parse your XML data. 将这些XML数据粘贴到名为yourxmlfile.xml的文件上,并使用simplexml_load_file()解析您的XML数据。 Using a foreach you can cycle through like this. 使用foreach您可以像这样循环遍历。

<?php
$xml = simplexml_load_file('yourxmlfile.xml');
foreach ($xml->dict->array->dict as $tag)
{
    if($tag[0]->key[3]=="rotation")
    {
        echo $tag[0]->real[2]."<br>";
    }
}

OUTPUT :

90
180
270

As said in the other answer, the key / (value) "pairing" is odd. 如另一个答案中所述, key / (value) “配对”是奇数。 However, you can use xPath for this as well: 但是,您也可以为此使用xPath:

$xml = new SimpleXMLElement($string);

$result = $xml->xpath("dict/array/dict/key[text()='rotation']/following-sibling::real");

while(list( , $node) = each($result)) {
    echo 'dict/array/dict/rotation: ',$node,"\n";
}

http://codepad.org/ib4NiBBz http://codepad.org/ib4NiBBz

Which gives: 这使:

dict/array/dict/rotation: 90
dict/array/dict/rotation: 180
dict/array/dict/rotation: 270

http://www.php.net/manual/en/simplexmlelement.xpath.php http://www.php.net/manual/en/simplexmlelement.xpath.php

You could accomplish it with an XPath expression: 您可以使用XPath表达式完成此操作:

$dom = new DOMDocument;
$dom->loadXML($xml);    
$xpath = new DOMXPath($dom);

$nodes = $xpath->query("//*[text()='rotation']/following-sibling::real/text()");

foreach ($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}

The XPath expression means: Find all <real> tags followed by any tag with the node value rotation and get their node value. XPath表达式的意思是:查找所有<real>标签,然后查找具有节点值rotation的任何标签,并获取其节点值。 An XPath expression allows you to have more control over the markup. XPath表达式使您可以更好地控制标记。 You can adjust the expression as you want. 您可以根据需要调整表达式。

Output: 输出:

90
180
270

Online demo 在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM