|
Posted by Jack on 06/16/07 23:23
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
[Back to original message]
|