Extract string from XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iLdn
Forum Newbie
Posts: 16
Joined: Sun Mar 14, 2010 5:04 pm

Extract string from XML

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Extract string from XML

Post 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;
iLdn
Forum Newbie
Posts: 16
Joined: Sun Mar 14, 2010 5:04 pm

Re: Extract string from XML

Post 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??:)
iLdn
Forum Newbie
Posts: 16
Joined: Sun Mar 14, 2010 5:04 pm

Re: Extract string from XML

Post 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?
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Extract string from XML

Post 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.
iLdn
Forum Newbie
Posts: 16
Joined: Sun Mar 14, 2010 5:04 pm

Re: Extract string from XML

Post 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!
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Extract string from XML

Post by timWebUK »

iLdn
Forum Newbie
Posts: 16
Joined: Sun Mar 14, 2010 5:04 pm

Re: Extract string from XML

Post 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?
PM2008
Forum Newbie
Posts: 7
Joined: Mon Dec 29, 2008 10:47 am

Re: Extract string from XML with biterscripting

Post 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.
Post Reply