简体   繁体   中英

php xml dom extract data from non-standard xml

Hello I have xml like this:

<specs><my>base</my><root>none</root></specs>
<books>

  <item>
    <id>14</id>
    <title>How to live</title>
  </item>

  <item>
    ...
  </item>

</books>

How can I extract value from < my > ? and then < title >?

when I have data such as : <specs><my>base</my><root>none</root></specs> in xml this code works for me. So how should I modify it to work with data such as books as well in xml?

$dom = new DOMDocument();
$dom->loadXml($xml);

$xpath = new DOMXPath($dom);
$entry = $xpath->query("//xml/specs/my");
foreach($entry as $ent){
  echo $ent->nodeValue;
}

simply I added this:

$xml="<xml>".$xml."</xml>";

and now this works $xpath->query("//xml/specs/my"); as well as $xpath->query("//xml/books/item");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM