943,753 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 920
  • PHP RSS
Jul 14th, 2009
0

extract and POST specific section of XML string?

Expand Post »
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.

php Syntax (Toggle Plain Text)
  1. //Response holds the xml returned from the third party service
  2. $xml = simplexml_load_string($response);
  3.  
  4. $toPost = $xml->xpath("Accommodations/AccommodationSegment[@ItineraryId='".$someVariable."']/descendant-or-self::*");
  5.  
  6. 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
Last edited by rickya100; Jul 14th, 2009 at 11:38 am. Reason: Spelling mistake
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Jul 15th, 2009
0

Re: extract and POST specific section of XML string?

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.
Reputation Points: 49
Solved Threads: 22
Junior Poster
Menster is offline Offline
175 posts
since Jun 2009
Jul 15th, 2009
0

Re: extract and POST specific section of XML string?

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
xml Syntax (Toggle Plain Text)
  1. <sampleelement>
  2. <subelement>Piece of info</subelement>
  3. </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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008
Jul 15th, 2009
0

Re: extract and POST specific section of XML string?

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:
php Syntax (Toggle Plain Text)
  1. $xmlString = $xml->asXml();
  2. $elements = array();
  3.  
  4. $element = "[I]Name of element you want[/I]";
  5.  
  6. $first_occurence = strpos($xmlString,$element);
  7. //ignore up till there
  8. $tempstr = substr($xmlString,intval($first_occurence+ strlen($element) + 1);
  9. $second_occurence = strpos($tempstr,$element);
  10. $attribute_value = substr($tempstr,0,$second_occurence);
  11.  
  12. $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
Reputation Points: 49
Solved Threads: 22
Junior Poster
Menster is offline Offline
175 posts
since Jun 2009
Jul 15th, 2009
0

Re: extract and POST specific section of XML string?

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
rickya100 is offline Offline
78 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Netbeans: code completion not working well
Next Thread in PHP Forum Timeline: session_destroy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC