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
https://forums.phpfreaks.com/topic/229468-extracting-data-from-xml/
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.

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));
}
?>

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

 

 

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 />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.