Jump to content

Extracting data from XML


brucegm

Recommended Posts

Hi there

 

I need to extract data from some XML. I have found a few sites that explain that part to me, however there is a section of the data which I need to extract and am not sure how to go about it. Below is an extract of the XML, the section I am trying to extract is highlighted in red (it is essentially a text message being sent I am trying to get the contents of the sms/text message):

 

<usa_smpp_host>THTTP</usa_smpp_host>

<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message</short_message>

<dest_addr_npi>1</dest_addr_npi>

 

Any assistance would be appreciated. Have played around quite a bit but have not managed to figure it out yet.

 

thanks

Link to comment
Share on other sites

strpos in combination with substr. 

 

This assumes that you will have the entire string inside that is currently inside short_message. 

 

strpos will get the starting point (looking for 'text:' and substr will use that number to get you the piece of the string that starts there, up until the end of the string. 

 

Manual pages at php.net for each should be all you need.

Link to comment
Share on other sites

Using what gizmola said you should produce something like this:

 

<?php
$doc = new DOMDocument();
$doc->loadXML('<root><item><usa_smpp_host>THTTP</usa_smpp_host>
<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message</short_message>
<dest_addr_npi>1</dest_addr_npi></item></root>');
$items = $doc->getElementsByTagName('short_message');
for ($i = 0; $i < $items->length; $i++) {
    $short_message = $items->item($i)->nodeValue;
    var_dump(substr($short_message,strpos($short_message, 'text:')+5));
}
?>

Link to comment
Share on other sites

Thanks! It works perfectly. I am a bit out of my depth on this - I am trying to loop through multiple records and pick up a few of the node values. By editing your script I get all the values, but not grouped by record (if that makes sense).

 

So for example, with this XML:

 

<root><item><usa_smpp_host>THTTP</usa_smpp_host><source_addr>123456</source_addr>

<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 1</short_message>

<dest_addr_npi>1</dest_addr_npi></item></root>

<root><item><usa_smpp_host>THTTP</usa_smpp_host><source_addr>987654</source_addr>

<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 2</short_message>

<dest_addr_npi>1</dest_addr_npi></item></root>

<root><item><usa_smpp_host>THTTP</usa_smpp_host><source_addr>554647</source_addr>

<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 3</short_message>

<dest_addr_npi>1</dest_addr_npi></item></root>

 

So I need the source_addr and the text from short_message. By editing your code I got this:

 

123456

987654

554647

 

Test SMS message 1

Test SMS message 2

Test SMS message 3

 

How do I loop through the XML to get the data returned like this:

 

123456

Test SMS message 1

 

987654

Test SMS message 2

 

554647

Test SMS message 3

 

 

Link to comment
Share on other sites

Ill let the code speak for itself, im sure you know whats potting. I am assuming only one item, if there are more than one you would have to loop through the items in the foreach for each element.

 

This is the full example outputting all the info, just change it to your needs

<?php
$doc = new DOMDocument();
$doc->loadXML('
<root>
<item>
	<usa_smpp_host>THTTP</usa_smpp_host>
	<source_addr>123456</source_addr>
	<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 1</short_message>
	<dest_addr_npi>1</dest_addr_npi>
</item>
<item>
	<usa_smpp_host>THTTP</usa_smpp_host>
	<source_addr>987654</source_addr>
	<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 2</short_message>
	<dest_addr_npi>1</dest_addr_npi>
</item>
<item>
	<usa_smpp_host>THTTP</usa_smpp_host>
	<source_addr>554647</source_addr>
	<short_message>id:660352946 sub:001 dlvrd:001 submit date:1102081032 done date:1102081035 stat:DELIVRD err:000 text:Test SMS message 3</short_message>
	<dest_addr_npi>1</dest_addr_npi>
</item>
</root>');
$items = $doc->getElementsByTagName('item');
foreach($items as $item){
$use_smpp_host = $item->getElementsByTagName('usa_smpp_host')->item(0)->nodeValue;
$source_addr = $item->getElementsByTagName('source_addr')->item(0)->nodeValue;
$short_message = $item->getElementsByTagName('short_message')->item(0)->nodeValue;
$part_of_short_message = substr($short_message,strpos($short_message, 'text:')+5);
$dest_addr_npi = $item->getElementsByTagName('dest_addr_npi')->item(0)->nodeValue;

echo $use_smpp_host."<br />";
echo $source_addr."<br />";
echo $short_message."<br />";
echo $part_of_short_message."<br />";
echo $dest_addr_npi."<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.