PHP - Parsing XML object created by simplexml_load_string()

I am trying to read an attribute value of the root node of an XML fed into the simplexml_load_string() method in PHP.

Here is an example,

<data status="Active"> <user> <userid> 1 </userid> </user> <user> <userid> 2 </userid> </user> <data>

I am trying to read the value of 'status' in the above XML string:

echo $xml->user[0]->userid; echo $xml->attributes()->status

The statements don't work too, var_dump() outputs "NULL".

-------------Problems Reply------------

echo (string)$userNode->attributes()->status should work

Besides the attributes() method you can simply access the element's attributes by treating the element object like an array (SimpleXMLElement implements ArrayAccess)

$xml = simplexml_load_string('<user status="Active"></user>');
echo 'status=', $xml['status'];

But to access attributes in other namespaces you have to use attributes() (afaik).

$xml = simplexml_load_string('<user status="Active"></user>');

echo $xml->attributes()->status;

Thanks for the replies... Parsing was not really the problem.

I missed the following cURL parameters,

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

I was treating a boolean value (cURL response) as an XML file :)

Thanks all...

Sorry but in your example XML you did not close your end <data> tag.

Category:php Views:0 Time:2010-09-23

Related post

  • Parsing XML and creating objects in Objective C 2011-03-09

    So I have the following String: "<Object> <attribute1>a1</attribute1> <attribute2>a2</attribute2> <attribute3>a3</attribute3> </Object>" I want to use that to initialize my Object with the three attribu

  • Array gets overwritten when parsing XML (Objective-C) 2011-07-04

    I'm using the TBXML parser but having some difficulties. I use the following code to add data from the product object to an MutableArray called laarzen. - (void)loadURL { // Load and parse an xml string Products *product = [[Products alloc] init]; tb

  • Problem parsing XML objective C code 2011-07-12

    I'm trying to develop an app that gets four coord. values from an XML file. The XML file is OK: <boa1> <latitudine>43.10123355297304</latitudine> <longitudine>12.31807708740234</longitudine> </boa1> <boa2>

  • How to navigate a XML object in Ruby 2011-07-01

    I have a regular xml object created from a response of a web service. I need to get some specific values from some specific keys... for example: <tag> <tag2> <tag3> <needThisValue>3</needThisValue> <tag4> <needT

  • Parsing XML files using CodeIgniter? 2010-11-23

    Searching the, Internet ,I found http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/, but I have a lot of questions. I am new to codeigniter, and I am trying to adapt my code to this xml. I want to display once

  • Parse XML in Js for Website 2011-10-11

    Hy! I am very new in Web-Dev. I have a mysql db and the controller is made with PHP. I give the data formated in php back to the html. My Quest: How to parse the xml in js right for the HTML based view? If there are better solutions for the data tran

  • parse xml from another domain with php 2012-02-20

    I want to parse a xml document wich is located on another domain. I want to do this with php. Is it possible? I've already try simpleXML but it doesn't seems to work. --------------Solutions------------- WEll first of all: http://www.stevymahy.com/xm

  • Return Objects created while parsing an XML Document 2011-12-14

    So I want to read data from an xml file, I am now at a point where I have XMLReader and ContentHandler in place and when the endDocument() is fired I have "collected" all the data I need from the document. But now it seems that I ran into a wall... H

  • Using SimpleXML to create an XML object from scratch 2008-09-27

    Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML

  • Return an object after parsing xml with SAX 2010-06-08

    I have some large XML files to parse and have created an object class to contain my relevant data. Unfortunately, I am unsure how to return the object for later processing. Right now I pickle my data and moments later depickle the object for access.

  • Parsing XML using libxml2 and Xpath in objective c 2010-10-26

    I want to parse an xml file using libxml2 and XPath I have tried to look this up on the internet but I can't find much about it. Can anyone give me a sample code for parsing this xml: <WebServiceResponse> <status>OK</status> <res

  • Problem: Parsing XML File in Objective-C/Cocoa Project for iOS 2010-11-11

    after searching days for a solution solving my errors, I want to share the whole problem with you. Maybe somebody have a better Idea to solve this. My Project uses a TabViewController to switch between Views. For every Tabview I load a separate class

  • What tests should I create for unit testing a library that receives and parses XML? 2010-12-16

    I'm trying to get into the groove of TDD and I've decided to refactor my entire class library project that parses XML it receives from an exposed API. Now I'm stuck on what tests I should have. Can you recommend some tests I should have and also how

  • ANTLR converting Java file to XML and Read back XML to create Java Object Issue 2011-04-06

    Ok... this is what I want to do..... Generate Java classes from some expression language.... Assume Step#1 is done... I have Java file. I have created JavaLaxer and JavaParser using Java.g file after modifying Java.g little bit (http://jumbleagileman

  • how to parse XML in objective c and generated comma separated string 2011-05-16

    i am trying to parse xml in objective c.my xml has following node <Item>Male</Item> <Item>Female</Item> </RadioButton> <Item>i-pad</Item> <Item>i-pod</Item> </RadioButton> now i want to gene

  • Parsing XML data to user defined objects, SAX or DOM 2011-11-14

    I want to create a simple function as follow: vector <User> convertXMLDataToUserList (string xmlData) { …. } Let's say that the xmlData is something as follow: <users> <user> <firstname>ABC</firstname> <lastname>DE

  • how to parse xml into java object? 2012-01-16

    Hi i have an xml in the following format generated from a web service. <root> <item> <name>test</name> <description>test description</description> <link>http://xxx</link> </item> </root> I r

  • android parse xml - Nodelist has too many objects 2012-02-21

    I had this working, made a change somewhere and now I can't track down the error. Here's the situation... when _regionList creates, there are 13 regions in the xml file for the region I am using but the _regionlist/children/array has 18 objects. Usin

  • Using Linq, how to parse Xml to C# objects which only accept paramaters in the constructor? 2012-04-16

    Suppose I have some XML Like this: <User> <Name>X</Name> <Gender>Y</Gender> <ImageUrl>Z</ImageUrl> </User> and I have a class called User. public class User { public User(string name, string gender, str

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.106 (s). 11 q(s)