开发者

XPath in PHP4 and value contents

开发者 https://www.devze.com 2023-02-02 23:55 出处:网络
I\'m trying to get the contents of the XML: $xmlstr =开发者_如何学Python \"<?xml version=\\\"1.0\\\" ?>

I'm trying to get the contents of the XML:

$xmlstr =开发者_如何学Python "<?xml version=\"1.0\" ?>
           <article>
             <Art>
               <test>Hello</test>
             </Art>
             <Another>
               <g>gooo</g>
             </Another>
           </article>";
$dom    =domxml_open_mem($xmlstr);


$calcX = &$dom->xpath_new_context();

$cnt = $calcX->xpath_eval($querystring);

foreach ($cnt->nodeset as $node)
{  
    print_r($node);

} 

Is there a way that I can get the content when the querystring is //article/Art?

What I'm looking for is:

<test>Hello</test>

If I use $node->get_content(), then the result is Hello.

I'm working with PHP4, so I'm unable to use SimpleXML [which is PHP5]. $node is DOMElement.

$node->nodeValue causes: Notice: Undefined property: nodeValue in .php on line 19


Is there a way that I can get the content when the querystring is //article/Art?

What I'm looking for is:

<test>Hello</test>

Use:

/article/Art/*

This means: Select all elements that are children of an Art element that is a child of the top element named article .

If you want all nodes below /article/Art, use:

/article/Art/node()

This selects all elements, text-nodes, comment nodes and processing-instruction nodes that are children of the top element named article .


It's been awhile that I used the old PHP4 DOM extension, but try

  • DomNode->dump_node - Dumps a single node

Example:

foreach ($cnt->nodeset as $node) {  
    echo $node->dump_node; 
}

If the above doesn't do what you are looking for, try the other dump_* methods in DomDocument.

0

精彩评论

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