Jump to content

Difficulty Pasing XML with PHP


labiere33
Go to solution Solved by Eiseth,

Recommended Posts

I've been trying to figure out how to parse an xml file using PHP and to output some HTML.

 

I followed an example from joemarini.com

 

Display a List of Categorized Links with PHP, XML, and SAX

 

The example xml being parsed is only showing tags with attributes, but not with text values within the tag pairs. So I see how to apply the php example to xml that looks like this:

<tagname>
   <tag1 attr="foo">
      <tag2 attr1="one" attr2="two"></tag2>
   </tag1>
</tagname>

but not to xml that looks like this:

<tagname>
   <tag1 attr="foo">
      <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2>
   </tag1>
</tagname>

Basically I want to modify the php example so that it allows me to not only store and do something with the attribute values as it parses, but also the text values between the tag pairs. (Is this what is being called the CDATA everywhere I look?) I have found examples that parse the values between tags and they look very different from the code example I have here that parses attribute values.

 

Any help is appreciated.

 

 

links.php

links.xml

Link to comment
Share on other sites

Are you trying to output the whole xml?

 

try DOMDocument()

// if you're getting the xml from outside source use file_get_contents('http://www.example.com');
$xml = <<<EOT
<?xml version="1.0" ?>
<tagname>
   <tag1 attr="foo">
      <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2>
   </tag1>
</tagname>
EOT;

$dom = new DOMDocument();
$dom->loadXML($xml);

header('Content-type: text/xml');
echo $dom->saveXML();

Are you trying to parse specific node?

Try simplexml_load_file

$xml = <<<EOT
<?xml version="1.0" ?>
<tagname>
   <tag1 attr="foo">
      <tag2 attr1="one" attr2="two">SOME TEXT VALUE ADDED HERE</tag2>
   </tag1>
</tagname>
EOT;

$sx = simplexml_load_string($xml);
echo $sx->tag1->tag2;

// output
SOME TEXT VALUE ADDED HERE

If this isn't what you want, then try playing with DOMDocument, DOMXPath, DOMElement, SimpleXML instead of parsing it using the tutorial you posted

Edited by Eiseth
Link to comment
Share on other sites

I have a web page calling a PHP script that is trying to read an xml file from an external source and do something with the values, like display them in an HTML table or maybe insert them into a mySQL database. But I want to display both the tag attribute values and values between tags. So something like:

<?xml version="1.0" ?>
<item status="active">
   <desc language="en">WIDGET</desc>
</item>

could be parsed into an HTML table like this:

<table>
   <tr>
      <td>active</td>
      <td>en</td>
      <td>WIDGET<td>
   </tr>
</table>

So yes, I want to parse the entire xml file, creating a new table row in my output table for each "Item" tag I encounter.

Link to comment
Share on other sites

  • Solution

Well you can use simplexml_load_string() or simplexml_load_file()

$xml = <<<EOT
<?xml version="1.0" ?>
<item status="active">
   <desc language="en">WIDGET</desc>
</item>
EOT;

// Use file if your xml comes from a file
// this converts your xml to object, so $xml == your root element <item>
$xml = simplexml_load_string($xml);

$item_attributes = $xml->attributes();
echo $item_attributes['status']; // active

$desc_attributes = $xml->desc->attributes();
echo $desc_attributes['language']; // en

echo $xml->desc; // WIDGET
Link to comment
Share on other sites

Sorry I only seem to be able to mark one of those last two replies as "best". Just to help anyone else out looking for similar information I thought I would add,

 

The example I quoted above seems to be a little dated. I actually ran across it from Lynda.com in their "XML Basics" video tutorial.  Understandably, no one really explained how to modify that example. SimpleXML that became available in PHP 5 seems to be an easier way to achieve the goal. I have latched on to the Basic Tutorial at php net and I should be able to help myself further along now.

 

Thanks.

Edited by labiere33
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.