使用php从xml文件中获取标题


Get title from xml file using php

我有这个xml文档,知道名字,我想要得到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>

我不知道如何使用xml文件,所以我需要一个php函数的例子,该函数返回该文档中具有name=Promotii的项目的id。当然,我的原始文件有更多的项目,但这是我感兴趣的部分。我的文件名为menu.xml

谢谢!

尝试以下一个,但x3ro是正确的。你需要展示你的努力不管是什么

<?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>";
    }
}