Hi everyone,

The goal is to be able to extract a specific element (and all children of it) from a XML string and then to echo this to the page inside a hidden form element so that it can be posted to another page.

I have tried to get the XML chunk printed to the page but cannot get it. I have got the XPath to select the chuck of XML I want but what I can't figurer out is how to then print this to the page.

//Response holds the xml returned from the third party service
$xml = simplexml_load_string($response);

$toPost = $xml->xpath("Accommodations/AccommodationSegment[@ItineraryId='".$someVariable."']/descendant-or-self::*");
						
echo htmlentities($toPost);

Nothing gets printed out for the above code. I can however do a print_r() on the $toPost variable and see the SimpleXML object.

But I really need just the chunk of XML that the Xpath selected.

Thank you to anyone who can help me with this.

Richard

Hi there,
Once the xpath function has run, you can treat the $topost variable as an array. So I'd recommend taking a look at the output of print_r($topost) and then make note of the ids of the elements that you want and extract them like that. That is if your $topost is gonna have the same format every time.

Hey Menster,

I have looked at the $toPost variable using print_r() but the thing is I don't need just some of the information from the XML I need all of it including the element tags as well.

So I need

<sampleelement>
<subelement>Piece of info</subelement>
</sampleelement>

And not just 'piece of info'

I have been using the simpleXML objects throughout my site in the way you mention but it isn't going to be sufficient this time for me to just be able to access the text info in the nodes I need everything.

Do you know of a way to convert a simpleXML object back into it's native XML structure and then be able to print it as a string?

Thanks for replying btw,


Richard

I've never really done too much work with XML but I think the function that you are looking for could be asXml(). I know its not exactly what you need but you could use some cunning string manipulation with php to get the elements out:

$xmlString = $xml->asXml();
$elements = array();

$element = "[I]Name of element you want[/I]";

$first_occurence = strpos($xmlString,$element);
//ignore up till there
$tempstr = substr($xmlString,intval($first_occurence+ strlen($element) + 1);
$second_occurence = strpos($tempstr,$element);
$attribute_value = substr($tempstr,0,$second_occurence);

$elements[$element] = $attribute_value;

Not really geared for any xml document, (and the indexes may each be off by one) but its the route I'd take.

Good luck

Thanks Menster,

I have seen the asXML floating about on a few tutorials but none of them really when into it. I'll look it up and try and work something.

I'm really surprised actually that it isn't a common use case and that there isn't a function for it but then PHP can't be perfect can it :-)

Thanks very much for the code I'll check it out. We have decided to temporarily go another route on the site to give me more time to work around this problem.

All the best,

Richard

This article has been dead for over six months. Start a new discussion instead.