Jump to content

Extract simple values from XML


Stal

Recommended Posts

Hi, I am looking for a solution to this problem I am faced with.

 

I have a website which i can query and it replies with a small amount of xml data with regards to a particular product, eg spec, weight, stock etc.  I am trying to create a small script that will query that page and extract the values of the xml response, eg the xml data may look like this:

 

<?xml version="1.0" ?> 
- <product>
  <result>OK</result> 
    <description>SOME PRODUCT</description> 
  <extendeddescription>TECHNICAL SPEC</extendeddescription> 
  <weight>9.35</weight> 
  <stock>434</stock> 
  </product>

 

I need to capture this response and extract the values of description, stock, etc as variables i can use in PHP.  Then i can call this page for each product code required, eg www.mydomain.com/script.php?product_code=12345

 

Can anyone point me in the right direction as i am struggling to find any info as to how to extract the values i need.

 

??? ??? ???

 

Thanks  ;D

Link to comment
Share on other sites

ok, how about another possibility.

 

Is there a PHP function i can use to extract everything between 2 values, eg:

 

$description = strip everything bewteen "<description>" and "</description>" from $xml_result;
$weight = strip everything bewteen "<weight>" and "</weight>" from $xml_result;

etc....

 

???

Link to comment
Share on other sites

I would use the SimpleXML object. Its dead simple to use like this:

Check out the php manual for detailed info.

 

DomXML is soooooooooo php4 :P

 

<?
$xml = <<<XML
<?xml version="1.0" ?> 
<products>
  <product>
    <result>OK</result> 
    <description>Product 1</description> 
    <extendeddescription>TECHNICAL SPEC</extendeddescription> 
    <weight>9.35</weight> 
    <stock>434</stock> 
  </product>
  
  <product>
    <result>OK</result> 
    <description>Product 2</description> 
    <extendeddescription>TECHNICAL SPEC</extendeddescription> 
    <weight>5.35</weight> 
    <stock>132</stock> 
  </product>
</products>
XML;

$xmlobj = simplexml_load_string($xml);

foreach ($xmlobj as $product) {   
  print (
    $product -> result."<br />".
    $product -> description."<br />".
    $product -> extendeddescription."<br />".
    $product -> weight."<br />".
    $product -> stock."<br /><br />"
  );
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.