By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

Need help getting values with simplexml

P: n/a
I'm writing a routine that fetches XML and attempts to parse some
values from it.
A condensed entry node of the XML looks like this ..

<entry>
<title>Some title</title>
<author>
<name>Author</name>
</author>
<source:resource url="http://somesite.com?tid=123456"/>
</entry>

I am able to get the values for things like title and author->name
but I need to get the value of the attribute for source:resource,
namely 123456 in this case

What I have so far.. with the above assigned as $xmlstr ..

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ($xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOEMPTYTAG))
  3. {}else{
  4. echo 'Something isn\'t right.'; exit();
  5. }
  6.  
  7. foreach ($xml->entry as $entry) {
  8. $title = $entry->title;
  9. $name = $entry->author->name;
  10. /*$tid =  presumably it is somehow accessable with SimpleXMLElement-
  11.         
  12.                 >attributes
  13.  
  14. but the parser seems to dislike the colons in the tag*/
  15. }
  16.  
  17.  
I was hopeing LIBXML_NOEMPTYTAG would help, but doesn't seem to.

Any hints ?
Nov 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
du**********@gmail.com escribió:
I'm writing a routine that fetches XML and attempts to parse some
values from it.
A condensed entry node of the XML looks like this ..

<entry>
<title>Some title</title>
<author>
<name>Author</name>
</author>
<source:resource url="http://somesite.com?tid=123456"/>
</entry>

I am able to get the values for things like title and author->name
but I need to get the value of the attribute for source:resource,
namely 123456 in this case
Tags with colons, like <source:resource>, use namespaces. Many XML
parsers have poor support for namespaces, or none at all. As far as I
know SimpleXML can handle namespaces but the documentation is not very
clear. I had to parse such XML not long ago and I couldn't manage to do
it with SimpleXML so I can't provide you with working code but I'll tell
you what I learnt in case it helps.

1) You need to have somewhere in your XML an attribute like this:

xmlns:source="some-unique-id"

I guess your full XML file has it. This prevents the "namespace error"
warning in simplexml_load_string().

2) Supposedly, you need to use the children() method to get elements
that belong to a namespace:

$children = $entry->children('source', TRUE);

3) Once you have the element, you need to fetch the URL as *attribute*.
This works for me when there aren't namespaces:

<foo bar="blah"---$xml->foo['bar']

See also the attributes() method.
I was hopeing LIBXML_NOEMPTYTAG would help, but doesn't seem to.
According to docs this option *removes* blank nodes:

http://es.php.net/manual/en/libxml.constants.php

As I said, these are only general ideas. I've never made it work myself.
If it's an option, you might consider other tools like XMLReader or a
PHP port of JavaScript's jQuery library called phpQuery:
http://code.google.com/p/phpquery/

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Nov 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.