php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34839 SimpleXMLElement too closed
Submitted: 2005-10-12 19:58 UTC Modified: 2020-02-18 04:08 UTC
Votes:2
Avg. Score:3.5 ± 1.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: stochnagara at hotmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: * OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stochnagara at hotmail dot com
New email:
PHP Version: OS:

 

 [2005-10-12 19:58 UTC] stochnagara at hotmail dot com
Description:
------------
The class SimpleXMLElement is wonderful and provides a great functionality.
But I need to extend it and I don't have a way to determine whether a given SimpleXMLElement is an attribute, an element, a CDATA section and so on.

So I suggest adding methods getName() and getType(). getName() returns element's name for elements, attribute's name for attributes and so on. getType() returns SimpleXMLElement::ELEMENT and so on.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 23:12 UTC] cmb@php.net
-Package: Feature/Change Request +Package: SimpleXML related
 [2020-02-18 04:08 UTC] carusogabriel@php.net
-PHP Version: 5.1.0RC1 +PHP Version: *
 [2024-05-16 06:05 UTC] rena168carper at outlook dot com
Hey There!!!

Here is a solution to your issue:

To determine whether a given SimpleXMLElement is an attribute, an element, a CDATA section, etc., you can use the following methods:

Check if it’s an Attribute:
Use the attributes() method to check if the SimpleXMLElement object has any attributes.
If the method returns a non-empty result, it’s likely an attribute.
PHP

$element = new SimpleXMLElement($xmlString);
if ($element->attributes()) {
    // It's an attribute
}
AI-generated code. Review and use carefully. More info on FAQ.
Check for CDATA:
When you cast a SimpleXMLElement to a string, it automatically returns the CDATA content.
If you need to preserve CDATA when loading the XML, use the LIBXML_NOCDATA option with simplexml_load_string.
PHP

$xml = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
$content = (string) $xml; // This will be the CDATA content
AI-generated code. Review and use carefully. More info on FAQ.
Get Element Name: (https://github.com)(https://www-lhiproviderportal.com)
Use the getName() method to get the name of the element.
PHP

$elementName = $element->getName();
AI-generated code. Review and use carefully. More info on FAQ.
Determine Element Type:
PHP does not have a built-in method like getType() for SimpleXMLElement.
However, you can infer the type based on the context and the methods used.
For example, to check if a SimpleXMLElement is a CDATA section, you can compare the string cast of the element with its asXML() output. If they differ, and the asXML() contains <![CDATA[, it’s a CDATA section.

Best Regards 
Rena Carper
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 20:01:29 2024 UTC