Jump to content

XML parser in PHP


ali_kiyani

Recommended Posts

Hi,

 

I want to read an XML file which can be reached here.

 

http://www.mazda.ca/eng/Vehicles/Mazda3/sedan_content/Mazda3_subnav02_content04.xml

 

I looked for XML parsers on Google and found several results which were using PHP Expat to do the job. One of them is this one:

 

http://www.linkstraffic.net/programming/php/xml/xmlparser.php

 

But I found out that to parse any XML file you must know its structure and tags. That is each parsers will be tailor made to read that particular XML file. Now my questions are:

 

1. Is there a generic parser available that will read all nodes and data?

 

2. Looking at the XML file above which I want to read can you suggest one or two nodes which I can used in PHP? I am a little confused because all the examples are using simple XML files with tags like <author> and <title> e.t.c but my XML file has tags like following:

 

<tr class="row_heading">

<tr class="row_odd">

<td align="left">

 

So tell me shall I use tag <tr> or <tr class="row_heading">

Link to comment
Share on other sites

You may use PEAR's XML_Unserializer library if you want.

 

If you want to parse attributes of a node you just set an option to true and it's okay.

 


$unser = new XML_Unserializer(array("parseAttributes" => true));

$status = $unser->unserialize($xmlString);

if (PEAR::isError($stats)) {
   die("Xml Unserialize error");
}

$resultArray =  $unser->getUnserializedData();

 

You can also tell the parser to put the attributes into a predefined array by setting another option. And with getUnserializedData(), you get the mutli-dimensional associative array.

 

Hope that helps.

Link to comment
Share on other sites

Thanks for the replies guys.

Let me try simplexml. In the mean time tell me is there some kind of recursive parser available? Because the document I am parsing has no fixed tags and they may increase or decrease. So I will be needing recursive parser if available the will go through all available nodes/tags.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.