-3

I have this xml document and knowing the name, I want to get the id:

<?xml version="1.0" encoding="UTF-8"?>
 <menu defaultLang="ro" lastId="38">
   <item>
     <id>1</id>
     <active>1</active>
     <name>
       <ro>Categories</ro>
     </name>
     <path>
       <ro>categories</ro>
     </path>
     <children>
       <item>
         <id>23</id>
         <active>1</active>
         <name>
           <ro>Proteins</ro>
         </name>
         <path>
           <ro>proteins-masa</ro>
         </path>
         <children></children>
         <image>Imag1.png</image>
       </item>
       <item>
         <id>38</id>
         <active>1</active>
         <name>
           <ro>Promotii</ro>
         </name>
         <path>
           <ro>promotii</ro>
         </path>
         <children></children>
         <image>promotions.png</image>
       </item>
    </children>
  </item>
</menu>

I don't know how to work with xml files, so I need an example of a php function that returns the id of the item with name=Promotii from this document. Of course, my original file has more items, but here is the part that I'm interested in. My file is called menu.xml

Thank you!

7
  • You aren't really showing a lot of effort here. The question is, what have you tried?. Please don't take this the wrong way, but this isn't a place where you get code written for free from strangers :) Commented May 29, 2013 at 11:41
  • Oh, and there are already 7,175 questions tagged XML + PHP, including this one which might be helpful... Commented May 29, 2013 at 11:45
  • Of course I have tried to do it alone before asking this questions, but I didn't do anything good, so I really need a little help. I don't think that this is very difficult but I really don't know how to do it. I haven't understood much from the examples I found on the internet. So if I don't ask to much I hope you could give me a short example. Thanks anyway Commented May 29, 2013 at 11:45
  • Then show us what you have tried. Everybody can say "yeah I've tried a lot and didn't succeed", but that doesn't show any effort. Show us what you have tried, and tell us what were the problems that you encountered. People will then help you to figure out where you went wrong. But just asking for a solution is just lazy, imho. Commented May 29, 2013 at 11:48
  • 1
    And please read this article: mattgemmell.com/2008/12/08/what-have-you-tried :D Commented May 29, 2013 at 11:49

1 Answer 1

0

Try with below one but x3ro is correct. You need to show your effort whatever it is.

<?php
$items = simplexml_load_file("filename.xml");
if(count($items)){
    $result = $items->xpath("//name[ro='Promotii']/preceding-sibling::id");
    //echo "<pre>";print_r($result);die;
    foreach ($result as $entry)
    {
        echo "<b>id</b>:".$entry;
        echo "<hr>";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! If you really want to tell you what I've tried, I was trying to get to that element, so I have tried foreach($xml->children() as $item)... and of course it returned me HTTP Error 500... Anyway, I've understood the method you've showed me. Thank again. Have a nice day

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.