php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31846 simplexml_load_string doesn't get root node
Submitted: 2005-02-04 16:28 UTC Modified: 2005-02-07 21:18 UTC
From: andrew at shh dot fi Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.0.3 OS: win32
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andrew at shh dot fi
New email:
PHP Version: OS:

 

 [2005-02-04 16:28 UTC] andrew at shh dot fi
Description:
------------
$xml = simplexml_load_string($xml_str) returns all tags correctly except the root node.

There appears to be NO WAY to get the the name of the root tag



Reproduce code:
---------------
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?><article><title>Hello</title></article>';

$simple = simplexml_load_string($xml);

print_r($simple);

Expected result:
----------------
The object should include the root tag, except it starts from child of the root


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-07 16:37 UTC] chregu@php.net
This is correct. print_r shows you the children nodes of the choosen node. in your code, $simple *is* the root node. I don't see anything wrong. There's no such thing as SimpleXMLDocument Object in SimpleXML.

And yes, there is no way of getting the name of the root tag. Use DOM if you need that.

 [2005-02-07 19:56 UTC] andrew at shh dot fi
So, using simpleXML, one can never know the root tag. Should we guess? The idea of an XML parser is to return ALL the nodes and not some of them. Take docbook - you have different root nodes depending on whether its simple or full. My point is, if you are having an xml parser, then it needs to read all nodes top-bottom, otherwise there is no point using it - and as you say correctly - might as well use DOM.
 [2005-02-07 19:58 UTC] andrew at shh dot fi
Will simpleXML ever be changed to include root nodes?
 [2005-02-07 21:18 UTC] amt@php.net
No. SimpleXML is not about being able to do 100% of XML. It's about doing 80% of XML with 20% of the effort.

Do this instead:

$rootTag = dom_import_simplexml($simple)->tagName;
 [2010-11-12 00:05 UTC] james at populiweb dot com
just in case it helps anyone, getting the name of the root node is possible:

$xml = simplexml_load_string($xml_str);
$xml->getName();
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC