homepage Welcome to WebmasterWorld Guest from 54.166.111.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XML with PHP5.3 extract data - newbie
Abbeycat




msg:4316777
 9:24 am on May 24, 2011 (gmt 0)

Hi, please forgive my ignorance but i am new to XML and its interaction with PHP5.3. I have experimented with simpleXMLElement but can only seem to extract data from the very top level of the data returned i.e. Transaction and Code. Perhaps i am up a gumtree!

All i need to get is the reply data to SMStext messages sent via a SMStext webservice. The data will then via PHP be fed back into the MYSQL database that logs outgoing and incoming SMS messages. The XML reply is in this format below. I have visited several web help pages but i am floundering. Just a simple line of code to loop through any replies would be most helpful.
Any ideas please?

Many thanks, an XML newbie!

XML:
<GetSMSReplyResponse>
<Transaction>
<Code>1</Code>
<Description>Transaction OK</Description>
</Transaction>
<Replies>
<Reply>
<ID>1</ID>
<ClientMessageReference>afk758</ClientMessageReference>
<PartTotal>1</PartTotal>
<Originator>-----bleeped------</Originator>
<Destination>------bleeped------</Destination>
<Date>2009-06-13</Date>
<Time>17:05:41</Time>
<Body>hello world</Body>
</Reply>
<Reply>
<ID>2</ID>
<ClientMessageReference>afk759</ClientMessageReference>
<PartTotal>1</PartTotal>
<Originator>------bleeped------</Originator>
<Destination>------bleeped------</Destination>
<Date>2009-06-13</Date>
<Time>17:06:37</Time>
<Body>Hello world</Body>
</Reply>
</Replies>
</GetSMSReplyResponse>

[edited by: httpwebwitch at 1:26 pm (utc) on May 24, 2011]
[edit reason] removed phone numbers from the example [/edit]

 

httpwebwitch




msg:4316850
 1:39 pm on May 24, 2011 (gmt 0)

once you've created your SimpleXML object, use print_r() to see its structure.

<?php
$obj = simplexml_load_string($xml);
print_r($obj);
?>

that method works well in cases like yours where you're not accessing data inside XML attributes and element values. When your data has mixed attributes and values, print_r() is ineffective because it converts your objects into arrays first. There's an explanation here:
[php.net...] and more good examples here: [php.net...]


Once you view the structure using print_r(), it should be easy to see how you get to the innards.

Something like this might work:

foreach ($obj->GetSMSReplyResponse->Replies->Reply as $reply) {
echo $reply->Body;
}

Abbeycat




msg:4317015
 5:50 pm on May 24, 2011 (gmt 0)

Many thanks, I'll look at this later.PH.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved