saipkjai Posted September 28, 2007 Share Posted September 28, 2007 hi I was trying to parse a segment of XML into plan html like the following <author> <name>John Doe</name> <email>johndoe@example.com</email> </author> TO <a href="johndoe@example.come">John Doe</a> and I have the following code for the parsing <?php $file = $_POST['url']; $authorItem = array(); $authorList = ""; function startElement($parser, $name, $attrs) { global $authorList; switch($name){ case "TITLE": echo "<h2>"; break; case "AUTHOR": $authorList .= "*$name"; break; case "NAME": $authorList .= "*$name"; break; case "EMAIL": $authorList .= "*$name"; break; } } function endElement($parser, $name) { global $authorList; global $authorItem; switch($name){ case "TITLE": echo "</h2>"; break; case "AUTHOR": if($authorList == "*AUTHOR*NAME*EMAIL"){ printf("<a href=mailto:%s>%s</a>", array_pop($authorItem), array_pop($authorItem)); $authorList = ""; } elseif($authorList == "*AUTHOR*NAME"){ printf("<p>%s</p>", array_pop($authorItem)); $authorList = ""; } break; } } function characterData($parser, $data) { global $authorList; global $authorItem; if($authorList == "*AUTHOR*NAME*EMAIL"){ array_push($authorItem, $data); for($i = 0; $i < count($authorItem); $i++){ echo "$i" . "$authorItem[$i]" . "\n"; } } elseif($authorList == "*AUTHOR*NAME"){ array_push($authorItem, $data); } else{ echo $data; } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen($file, "r"); while ($data = fread($fp, 4096)) { xml_parse($xml_parser, $data, feof($fp)); } xml_parser_free($xml_parser); ?> The problem that I'm facing now is, somehow, the array authorItem will store John Doe at [0], then blank at [1] and the email at [2]. Is there some step I got it wrong while parsing occur? Quote Link to comment https://forums.phpfreaks.com/topic/70984-solved-xml-parsing/ Share on other sites More sharing options...
sKunKbad Posted September 28, 2007 Share Posted September 28, 2007 Properly formatted xml file named name_email.xml: <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"> <content> <author> <name>John Doe</name> <email>johndoe@example.com</email> </author> </content> </rss> php5 script: <?php $feed = simplexml_load_file('name_email.xml'); $name = $feed->content->author->name; $email = $feed->content->author->email; echo "<a href='$email'>$name</a>"; ?> That's as easy as it gets. Quote Link to comment https://forums.phpfreaks.com/topic/70984-solved-xml-parsing/#findComment-356973 Share on other sites More sharing options...
saipkjai Posted September 28, 2007 Author Share Posted September 28, 2007 Thx for the reply!! I was surprise that it was this simple. But then I have another question Firstly, the version of XML file that I'm working with is version 1.0, does this method works with version 1.0? Secondly, the XML file that I'm working with has many many other entry, some of them have only <author><name></name></author>, some of the other have email tag between it as well, how can I use the simple method that you suggest for those entry? Thridly, some of the tag like <link /> does not have open and close tag, how can I retrieve the attribute of those tags? sample XML are the following <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <subtitle>A subtitle.</subtitle> <updated>2003-12-13T18:30:02Z</updated> <author> <name>John Doe</name> <email>johndoe@example.com</email> </author> <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link href="http://example.org/2003/12/13/atom03"/> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text & stuff. HTML looks like this: <p>.</summary> </entry> <entry> <title>Test entry #2</title> <author><name>Greg</name></author> <id>http://example.org/article2</id> <updated>2006-10-03T13:12:34Z</updated> <link href="http://example.org/article2" /> <content type="html">This is <em>very</em> exciting. &</content> </entry> <entry> <title>Test entry #3</title> <id>http://example.org/article3</id> <updated>2006-10-03T13:14:34Z</updated> <link href="http://example.org/article3" /> <content type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="http://www.cs.sfu.ca/~ggbaker/img/smiley" alt=":-)" /></xhtml:div> </content> </entry> <entry> <title>Test entry #4</title> <id>http://example.org/article4</id> <updated>2006-10-03T14:14:34Z</updated> <link href="http://example.org/article4" /> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"><p>This is also <em>very</em> exciting.</p> <p>Yes, it is.</p></div> </content> </entry> </feed> thx for the help again Quote Link to comment https://forums.phpfreaks.com/topic/70984-solved-xml-parsing/#findComment-356996 Share on other sites More sharing options...
Barand Posted September 28, 2007 Share Posted September 28, 2007 try <?php $xml = simplexml_load_file('myfeed.xml'); echo "<h3>{$xml->title}</h3><h4>{$xml->author->name}<br/>{$xml->author->email}</h4>\n"; foreach ($xml->entry as $entry) { echo "<table border='1' width='500'>\n"; echo "<tr><th>Title</th><td>{$entry->title}</td></tr>\n"; echo "<tr> <th>Content</th> <td>{$entry->content}"; if ($entry->content) foreach ($entry->content->div as $div) { echo "<div>"; foreach ($div->p as $p) echo "<p>$p</p>"; echo "</div>"; } echo "</td> </tr>\n"; echo "<tr><th>Summary</th><td>{$entry->summary}</td></tr>\n"; echo "<tr><th>Link</th><td>{$entry->link['href']}</td></tr>\n"; echo "</table><br /><br />\n" ; } ?> Content probably should be CDATA Quote Link to comment https://forums.phpfreaks.com/topic/70984-solved-xml-parsing/#findComment-357057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.