Re: xml parsing

From: Date: Mon, 12 Mar 2007 23:10:57 +0000
Subject: Re: xml parsing
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-250348@lists.php.net to get a copy of this message
Richard Lynch wrote:
I don't think that's valid XML, because you are mixing your content with your XML tags in a way that will confuse the two... If you can FIX the XML by using the CDATA stuff, or htmlentities encoding the HTML or something, that would be best. If you are STUCK with this bogus XML, you could probably write your own parser for something this simple/small in about an hour using http://php.net/strtok and friends... On Sun, March 11, 2007 7:38 pm, Marije van Deventer wrote:
I have been trying to parse this xml, and want to use it with childnodes <Label> and <Tekst>, but sofar due to the <li> and <p> and <image> elements no luck. How can i do this in a simple way ??? <?xml version="1.0" encoding="UTF-8"?> <Menu> <Item> <Label>Algemeen</Label> <Tekst>aaaaaaaaaaaaaaaaaaaaaaaaaaaaa</Tekst> </Item> <Item> <Label>1-Atmosfeer</Label> <Tekst>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <li>aaaaaaaaaaaaaaaaaaaaa</li> <li>bbbbbbbbbbbbbbbbbbbbbbbbbbb</li> <li>ccccccccccccccc</li> <li>dddddddddddddddddddddddddddddddddd</li> <img src="Saf2.jpg" alt="News" width="240" height="232"/><br /><br /><br />qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq<br /><br /><br /></Tekst> </Item> <Item> <Label>Betrouwbare drukcabine</Label> <Tekst>ffffffffffffffffffffffffffffffff<img src="Saf3.jpg" alt="News" width="180" height="239"/><br /><br /><br /><br /><br /><br /><br /><br />gggggggggggggggggggggggggggggggggggggggggggg<br /><br /><br /><br /><br /><br /></Tekst> </Item> </Menu>
    
I put the above xml text into an xml editor and it tests as valid, despite its raggedness, and I did manage to get it to parse with simplexml, using the following:
$string = the above xml; $xml = simplexml_load_string($string); foreach($xml->Item as $item ){ trim($item->Label); echo $item->Label . "<br>"; if(isset($item->Tekst)) {
       trim($item->Tekst);
      echo $item->Tekst . "<br>";
} } -- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/

« previous php.general (#250348) next »