1

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

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thats right and the xml is oddly formatted for the given task. you would think hey would have something like <rotation>VALUE</rotation> instead of having the value be the next sibling.
@Victory, Exactly, they don't have like attribute structure , so possible way would be something this.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.