-3

I have this XML file and i need to extract the values of USER_NAME and USER_ID and IP from it. I loaded the XML into SimpleXML using:

$xmlObj=new SimpleXMLElement($xmlstring);

But I don't now what to do after that..

<?xml version="1.0" encoding="UTF-8"?>
<content>
   <response>
      <state>
         <code>1</code>
      </state>
        <userinformationlist>
           <userinformation>
              <attribute key="USER_NAME">Charles</attribute>
              <attribute key="USER_ID">88477299101</attribute>
              <attribute key="IP">127.0.0.1</attribute>
              <attribute key="CCR_UUID" />
           </userinformation>
        </userinformationlist>
     </response>
</content>
Marcel Emblazoned
  • 593
  • 2
  • 4
  • 16

1 Answers1

1

This is an example using the DOM extension, particularly DOMXPath, rather than SimpleXML as I find SimpleXML on the whole unpalatable.

Example:

$dom = new DOMDocument();
$dom->loadXML($xmlstring);
$xpath = new DOMXPath($dom);

$name = $xpath->evaluate('string(//attribute[@key="USER_NAME"])');
$id   = $xpath->evaluate('string(//attribute[@key="USER_ID"])');
$ip   = $xpath->evaluate('string(//attribute[@key="IP"])');
echo "$name\t$id\t$ip\n";

Output:

Charles 88477299101 127.0.0.1
user3942918
  • 25,539
  • 11
  • 55
  • 67
  • Wonderful, thanks! I was unable to get it to work with SimpleXML no matter what i tried.. :( Cheers! – Marcel Emblazoned Dec 14 '14 at 15:18
  • @MarcelEmblazoned: Then this was what you were looking but (not search ing ;)) for: [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](http://stackoverflow.com/q/992450/367456) – hakre Dec 22 '14 at 10:36