473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help getting values with simplexml

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
1 4366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: archi | last post by:
dear all, what would be the most simple way to multisort the (HTML) output from a simplexml file. imagine i have loaded a simplexml file with names & addresses & much more, and i would like...
3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
7
by: SM | last post by:
Hello, I have an XML file that looks like this <?xml version="1.0" encoding="UTF-8"?> <discography> <CD> <title>Moonlight</title> <year>1974</year> <description>
2
by: alexus | last post by:
fwrite(fopen('temp.xml','w+'), print_r(simplexml_load_string(curl_exec($test))),true); i for some reason get "1" inside of my temp.xml
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.