|  | Posted by frodo2000@gmail.com on 06/17/07 09:11 
On 17 Cze, 01:23, Jack <accpac...@hotmail.com> wrote:> Hi,
 > I am trying to parse an xml file and I am having some problems. Here
 > is a sample structure of my xml file.
 >
 > <title>Example</title>
 >  <link rel="self" href="http://link0.com"/>
 >
 >  <entry>
 >    <title>E1</title>
 >    <link href="http://link1.com"/>
 >  </entry>
 >  <entry>
 >    <title>E2</title>
 >    <link href="http://link2.com"/>
 >  </entry>
 >
 > I want to be able to output the following
 >
 > <p><a href="http://link0.com'">Example</a></p>
 > <p><a href="http://link1.com'">E1</a></p>
 > <p><a href="http://link2.com'">E2</a></p>
 >
 > here is my code:
 > $file="test.xml";
 >
 > $xml_parser = xml_parser_create();
 >
 > $handle = fopen($file, "rb");
 > while (!feof($handle)) {
 >      $data .= fread($handle, 8192);}
 >
 > fclose($handle);
 >
 > xml_parse_into_struct($xml_parser, $data, $vals, $index);
 > xml_parser_free($xml_parser);
 >
 > foreach ($vals as $xml_elem) {
 >     if ($xml_elem['level'] == 1){
 > // THIS IS WHERE I SOMEHOW GOING TO READ AHEAD OR ACCESS THE NEXT
 > TAG=LINK AND SOMEHOW READ ITS ATTRIBUTE
 >         if ($xml_elem['tag'] == 'TITLE')
 >             printf ("<h1>%s</h1>\n", $xml_elem['value']);
 >         else if ($xml_elem['tag'] == 'SUBTITLE')
 >             printf ("<h3>%s</h3>\n", $xml_elem['value']);
 >     }
 >
 > The problem is write here because i don't know how I can first read
 > the LINK attribute first and then the title.
 >
 > Thanks
 
 I think that SimpleXML would be more convenient for your task. Below
 you have sample code:
 
 <?php
 $xml = 	"<test>".
 "<entry><title>Test1</title><link>www.test1.org</link></entry>".
 "<entry><title>Test2</title><link>www.test2.org</link></entry>".
 "</test>";
 
 $xmlTable = simplexml_load_string($xml); //Use
 simplexml_load_file($filename) for file loading
 
 for ($i = 0; $i < count($xmlTable->entry); $i++){
 echo '<p><a href="'.$xmlTable->entry[$i]->link.'">'.$xmlTable-
 >entry[$i]->title.'</a></p>';
 }
 ?>
 [Back to original message] |