0

I have a XML file and a for-each items, but I don't know how to get category name and subcategory name and also I can not edit the xml.

This is my XML file:

<?xml version='1.0' encoding='UTF-8'?>
<data>
  <category name="test-cat1">
    <subcat name="test-subcat1">
      <item name="test-name1">data1</item>
      <item name="test-name2">data2</item>
      <item name="test-name3">data3</item>
    </subcat>
    <subcat name="test-subcat2">
      <item name="test-name4">data4</item>
      <item name="test-name5">data5</item>
      <item name="test-name6">data6</item>
    </subcat>
  </category>
  <category name="test-cat2">
    <subcat name="test-subcat3">
      <item name="test-name7">data7</item>
      <item name="test-name8">data8</item>
      <item name="test-name9">data9</item>
    </subcat>
    <subcat name="test-subcat4">
      <item name="test-name10">data10</item>
      <item name="test-name11">data11</item>
      <item name="test-name12">data12</item>
    </subcat>
  </category>
</data>

This is my PHP script:

<?php

 $xml = simplexml_load_file('test.xml');

  foreach($xml->xpath('//item') as $item){

    echo $item . '<br>';
    echo $item['name'] . '<br>';
    echo /*item category*/;
    echo /*item sub-category*/;
    echo "<br>";

  }

?>

And i want to have this result:

data1
test-name1
test-cat1
test-subcat1

data2
test-name2
test-cat1
test-subcat1

...
1

2 Answers 2

1

If you use XPath with $item->xpath('ancestor::category')[0]['name'] and $item->xpath('parent::subcat')[0]['name'] then you should get the values:

  foreach($data->xpath('//item') as $item){

    echo $item . '<br>';
    echo $item['name'] . '<br>';
    echo $item->xpath('ancestor::category')[0]['name'] . '<br>';
    echo $item->xpath('parent::subcat')[0]['name'] . '<br>';
    echo "<br>";

  }
Sign up to request clarification or add additional context in comments.

Comments

0

Iterate through SimpleXMLElement::children() instead:

<?php

$xml = simplexml_load_file('test.xml');

foreach($xml->children() as $cat){
    $catName = $cat["name"];
    foreach($cat->children() as $subcat){
        $subcatName = $subcat["name"];
        foreach($subcat->children() as $child){
            echo "$child", "\n", $child["name"], "\n", $subcatName, "\n", $catName, "\n\n";
        }
    }
}

Gives this output:

data1
test-name1
test-subcat1
test-cat1

data2
test-name2
test-subcat1
test-cat1

data3
test-name3
test-subcat1
test-cat1

data4
test-name4
test-subcat2
test-cat1

data5
test-name5
test-subcat2
test-cat1

data6
test-name6
test-subcat2
test-cat1

data7
test-name7
test-subcat3
test-cat2

data8
test-name8
test-subcat3
test-cat2

data9
test-name9
test-subcat3
test-cat2

data10
test-name10
test-subcat4
test-cat2

data11
test-name11
test-subcat4
test-cat2

data12
test-name12
test-subcat4
test-cat2

Note that "$child" is used to convert $child into its string content. This is same as (string $child) or $child->__toString().

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.