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


How do I write a regular expression to get value from the XML node? Actually the node structure is very large so we can not easily pass, so I have to read as a normal text file and hopefully I can write a regex to find the matching elements.

  & lt; Node1 & gt; & Lt; Node2 & gt; Str & lt; / Node2 & gt; & Lt; Node3 & gt; Text & lt; / Node3 & gt; & Lt; Myvalue & gt; This value is a string .. & lt; / Myvalue & gt; & Lt; / Node1 & gt;  

The above patterns I & lt; Myvalue & gt; & Lt; / Myvalue & gt; , but in many other nodes in my XML, & lt; Myvalue & gt; The child is the only way to find the appropriate node which I want in the above pattern, the only change in the rest of the node values ​​is the same , & lt; Node3 & gt; Text & lt; / Node3 & gt; is the same. / P>

How do I write regex for phge?

Use an XML parser,

  • li>
  • (Posted by)
  • Here is a simple example with the DOM, which is located in myvalue and node1 .

      & lt ;? Php $ document = new DOMDocument (); $ Document- & gt; LoadXML ('& lt; all & gt; Otherwise & lt; myvalue & gt; anywhere & lt; / myvalue & gt; & lt; node1 & gt; & lt; node2 & gt; str & lt; / node2 & Gt; & lt; node3 & gt; text & lt; / node3 & gt; & lt; myvalue & gt; value string. & Lt; / myvalue & gt; & lt; / node1 & gt; & lt; / All & gt; '); $ Lst = $ document- & gt; GetElementsByTagName ('node1'); ($ I = 0; $ i & lt; $ lst- & gt; Length; $ i ++) for {$ node1 = $ lst- & gt; Item ($ i); $ Myvalue = $ node1- & gt; GetElementsByTagName ('myvalue'); If ($ myvalue-> length> gt; 0) {echo $ myvalue-> Items (0) - & gt; TextContent; }}? & Gt;  

    Comments