开发者

PHP Xpath help

开发者 https://www.devze.com 2023-01-17 09:24 出处:网络
Can anyone point me to a good tutorial on xpath or give me a quick lesson here? Also if xpath is for querying xml, what would I need to use to modify th开发者_开发知识库e xml? I guess it would need to

Can anyone point me to a good tutorial on xpath or give me a quick lesson here? Also if xpath is for querying xml, what would I need to use to modify th开发者_开发知识库e xml? I guess it would need to be something that works hand in hand with xpath?

Cheers, Franky


Here is a good overview of working with XML in PHP.

Instead of using XPath, I would recommend using SimpleXML and DOM to parse it. For example...

<?xml version="1.0" encoding="UTF-8"?>
<books> 
    <book> 
       <title>Great American Novel</title> 
       <characters> 
        <character> 
         <name>Cliff</name> 
         <desc>really great guy</desc> 
        </character> 
    </characters>
  </book>
</books>

You could parse by doing this (assuming XML is in $xmlstr)...

<?php 

  $xml = new SimpleXMLElement($xmlstr); 

  echo $xml->book[0]->title; // "Great American Novel" 
  ?> 


There's no "quick" introduction to XPath unless you give some example queries. I found the reference/tutorial from w3schools to be quite good.

To modify XML documents in PHP you can use SimpleXML or XMLReader/XMLWriter (and probably haps of other XML implementations in PECL)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号