Page 1 of 1

Extract string from XML

Posted: Tue Mar 16, 2010 3:11 pm
by iLdn
i'm trying to extract a string from a XML stream. The XML is the response of a GET request made with cURL.
This is the code of the cURL script

Code: Select all

<?php
     
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL,$url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/9.0.1 (Windows; Microsoft Windows XP Professional Service Pack 3 (Build 2600))");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");
        curl_setopt($ch, CURLOPT_COOKIE, "");
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         
  
        $data = curl_exec($ch); 
 
        if (curl_errno($ch)) { 
            print "Error: " . curl_error($ch); 
        } else { 
            // Show me the result 
            //var_dump($data);
            
            $GETxml = simplexml_load_string($data);
           $stringa = $GETxml->asXML();
            
            var_dump($stringa);
 
     [color=#FF4000]      $appname = "SOME";
            $cerca = "SOMETHING" . $appname . "/id";
            $codice = substr($stringa, $cerca, 9);
            echo "<br><br><br>" . $codice;[/color]
            curl_close($ch); 
        } 
?>
The GET request is perfect and works great, but i'm not able to search a string into the XML and the part evidenced in red doesn't work :(
Can you help me?

Re: Extract string from XML

Posted: Tue Mar 16, 2010 3:22 pm
by John Cartwright
It's not clear exactly the element you are trying to acquire, however, you can access the elements as member variables.

Code: Select all

$GETxml = simplexml_load_string($data);
 
echo $GETXML->foobar;
//or save as variable (need to cast to string for values otherwise you'll receive it as a simplexml object
$foo = (string)$GETXML->foobar;

Re: Extract string from XML

Posted: Tue Mar 16, 2010 3:45 pm
by iLdn
John Cartwright wrote:It's not clear exactly the element you are trying to acquire, however, you can access the elements as member variables.

Code: Select all

$GETxml = simplexml_load_string($data);
 
echo $GETXML->foobar;
//or save as variable (need to cast to string for values otherwise you'll receive it as a simplexml object
$foo = (string)$GETXML->foobar;
the element is a URL. In the XML i've a lot of data, name, general stuff, and a ubique URL... This way will works??:)

Re: Extract string from XML

Posted: Wed Mar 17, 2010 6:24 am
by iLdn
John Cartwright wrote:

Code: Select all

$GETxml = simplexml_load_string($data);
 
$foo = (string)$GETXML->foobar;
i can't understand this code... What i've to put for string? And what for foobar?

Re: Extract string from XML

Posted: Wed Mar 17, 2010 6:51 am
by timWebUK
$data would be your XML data that you wish to parse.

simplexml_load_string takes a well-formed XML string and stores it as an object (see the manual).

To access an element, you can reference it by replacing foobar with an element.

Here's an example:

Code: Select all

<?php
//HEREDOC containing XML
$xmlAddressBook = <<<XML
<?xml version='1.0'?> 
<addressbook>
    <name>John Smith</name>
    <phone>01232458987</phone>
    <address>20 Quiet Lane</address>
</addressbook>
XML;
 
//Load XML heredoc into a variable using simplexml_load_string
$getXML = simplexml_load_string($xmlAddressBook);
 
//Name
echo $xmlElement = (string)$getXML->name;
echo "<br />";
//Phone Number
echo $xmlElement = (string)$getXML->phone;
echo "<br />";
//Address
echo $xmlElement = (string)$getXML->address;
echo "<br />";
?>
Output:

John Smith
01232458987
20 Quiet Lane


Hope this helps.

Re: Extract string from XML

Posted: Wed Mar 17, 2010 8:48 am
by iLdn
Ok, it's clear..

But it's not ok for what i want to do.

i have an XML that contains something like that

Code: Select all

<GotoURL target="main" inhibitDragging="false" url="http://WEBSITE-EXAMPLE/id287965124" draggingName="SOMETHING">
 
I have to extract the id (id287965124). So not a value but an attribute!

Re: Extract string from XML

Posted: Wed Mar 17, 2010 8:56 am
by timWebUK

Re: Extract string from XML

Posted: Wed Mar 17, 2010 9:47 am
by iLdn
I tried this function but is not the right one for me. Infact i have several GotoURL different tag, but i want just one of them (the one that contains as an attribute the value that i need)

If there are no XML function i think that i can convert the XML to normal string and then do a substr()..
Can this way works? There's a way to convert the XML to "not XML formatted" text?

Re: Extract string from XML with biterscripting

Posted: Mon Apr 05, 2010 10:55 am
by PM2008
Input
<GotoURL target="main" inhibitDragging="false" url="http://WEBSITE-EXAMPLE/id287965124" draggingName="SOMETHING">

biterscripting code. I will assume your input is at "http://www.somesite.com/somefile.xml".

Code: Select all

# Script Id.txt
var str input, id
cat "http://www.somesite.com/somefile.xml" > $input
stex -r -c "^url&=&id&\"^" $input > $id
stex -c "^id^]" $id > null ; stex -c "[^\"^" $id > null
echo $id

See the regular expressions at http://www.biterscripting.com/helppages/RE.html .

You can customize it further.