Anni
Anni

Reputation: 85

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. 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

<?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

Upvotes: 1

Views: 1474

Answers (3)

Amal Murali
Amal Murali

Reputation: 76656

You could accomplish it with an XPath expression:

$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. An XPath expression allows you to have more control over the markup. You can adjust the expression as you want.

Output:

90
180
270

Online demo

Upvotes: 0

Jared Farrish
Jared Farrish

Reputation: 49208

As said in the other answer, the key/(value) "pairing" is odd. However, you can use xPath for this as well:

$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

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

Upvotes: 0

Paste those XML data on a file say yourxmlfile.xml and use simplexml_load_file() to parse your XML data. Using a foreach you can cycle through like this.

<?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

Upvotes: 1

Related Questions