开发者

PHP: regular expression to find out and retrieve values from xml attributes

开发者 https://www.devze.com 2023-01-17 21:16 出处:网络
How can I write a regular expression to retrieve values from xml node? Actually the node structure is very big. So we can\'t traverse easily, so I want to read as normal text file and hope I can write

How can I write a regular expression to retrieve values from xml node? Actually the node structure is very big. So we can't traverse easily, so I want to read as normal text file and hope I can write a regex to find out the matching elements.

<node1>
 <node2>str</node2>
 <node3>Text</node3>
 <myvalue>Here is the values string..</myvalue>
</node1>

The above is the pattern I want to retrieve values <myvalue></myvalue> but i开发者_C百科n my xml there are so many other node contains the <myvalue> child. So only way to find out the appropriate node which I want is in the above pattern. The only change in value rest of the node values are same <node2>str</node2>, <node3>Text</node3> are always same.

So how can I write the regex for php?


Use a XML parser, Regex is not appropriate to do that kind of parsing.

Here's the list of the XML parser you can use :

  • XMLReader
  • DOM (Example)
  • Simple XML (Example posted by Fanis)

Here's a simple example with DOM that will find all the myvalue located in the node1.

<?php
    $document = new DOMDocument();
    $document->loadXML(
        '<all>
            <myvalue>Elsewhere</myvalue>
            <node1>
                <node2>str</node2>
                <node3>Text</node3>
                <myvalue>Here is the values string..</myvalue>
            </node1>
        </all>');
    $lst = $document->getElementsByTagName('node1');

    for ($i=0; $i<$lst->length; $i++) {
        $node1= $lst->item($i);
        $myvalue = $node1->getElementsByTagName('myvalue');

        if ($myvalue->length > 0) {
            echo $myvalue->item(0)->textContent;
        }
    }
?>


PHP has a SAX-based XML parser which will let you use a real XML parser without storing an entire DOM tree in memory. XMLReader lets you parse the file without even reading the entire file into memory. Using regex to dig into XML is going to be painful.


If you insist on using regular expression for this, try

preg_match_all('<myvalue>([\s\S]+)<\/myvalue>', $text, $matches);
0

精彩评论

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

关注公众号