Jump to content

[SOLVED] Help with SimpleXML->attributes()


thomasgrant

Recommended Posts

I've searched Google for a bit this morning for examples without finding anything that applies to what I'm trying to accomplish.

 

Here is a sample of XML to get the idea out there.

 

<?xml version="1.0" encoding="UTF-8"?>
<newsletter>
  <year name="2007">
    <content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</content>
  </year>
  <year name="2008">
    <content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</content>
  </year>
</newsletter>

 

I've got the basics down of using SimpleXML in pulling XML data out and displaying on screen. But what I'm trying to do now is pull the content from a specific "year" based on the what I choose (ie. 2007, 2008, etc.) The examples I've seen thus far for using attributes() are typically while displaying everything in an array (ie. all years at once).

 

Can anyone provide some insight into my request?

 

Thanks in advance!

Link to comment
Share on other sites

I appreciate the quick response Daniel0. Unfortunately, that did not yield a positive result.

 

The output I get using your example is: "Array"

 

Then when I print the array,

 

$node = $doc->xpath("year[@name='2007']");
print_r ($node);

 

I get:

 

Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => 2007 ) [content] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ) )

Link to comment
Share on other sites

SimpleXMLElement::xpath() always returns an array. You'll have to get the first item in the array. You can do it like this:

list($node) = $doc->xpath("year[@name='2007']");

or

$node = $doc->xpath("year[@name='2007']");
$node = $node[0];

 

Then you'll have an instance of SimpleXMLElement. Then you can do

echo $node->content

. If you wish the content node directly then you can use the following XPath query instead:

year[@name='2007']/content

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.