Is really no one interested?

php.internals

Marian Kostadinov

20 years ago
This is related to feature request 34839. [12 Oct 7:58pm CEST] 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. Maybe it is late for 5.1.0 but 5.1.1 is a good place for this to appear.

Christian Stocker

20 years ago
On 31.10.2005 10:16 Uhr, Marian Kostadinov wrote:
> This is related to feature request 34839. > > [12 Oct 7:58pm CEST] 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.
SimpleXML is meant to be simple and the desicion was made to keep its API short. Use DOM if you want those more advanced features (there's dom_import_simplexml() and simplexml_import_dom() for easy and fast switching between those 2) chregu
> > > > > Maybe it is late for 5.1.0 but 5.1.1 is a good place for this to appear. >
-- christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71 http://www.bitflux.ch | christian.stocker@bitflux.ch | GPG 0x5CE1DECB

Marian Kostadinov

20 years ago
> SimpleXML is meant to be simple and the desicion was made to keep its > API short. Use DOM if you want those more advanced features (there's > dom_import_simplexml() and simplexml_import_dom() for easy and fast > switching between those 2) > > chregu
SimpleXML is simple and that is great. But such a CORE functionality like getting a SimpleXML node own name and type is not available. It's funny that one can access absolutely everything about a node children, attributes and so on very easily and intuitively, but cannot access node's own name:)

Petar Nedyalkov

20 years ago
On Monday 31 October 2005 11:27, Marian Kostadinov wrote:
> > SimpleXML is meant to be simple and the desicion was made to keep its > > API short. Use DOM if you want those more advanced features (there's > > dom_import_simplexml() and simplexml_import_dom() for easy and fast > > switching between those 2) > > > > chregu > > SimpleXML is simple and that is great. But such a CORE functionality > like getting a SimpleXML node own name and type is not available. > It's funny that one can access absolutely everything about a node > children, attributes and so on very easily and intuitively, but cannot > access node's own name:)
I don't think it's a core feature since the tree hierarchy of the XML is well defined and you have a well-formed DTD. As Christian spotted - the DOM functionallities could serve you well. The basic and simple API attracts so much users, let's keep it this way.
-- Cyberly yours, Petar Nedyalkov Devoted Orbitel Fan :-) PGP ID: 7AE45436 PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436

Marian Kostadinov

20 years ago
Who says that I have a well-formed DTD? DTD is not necessary in many cases. I will illustrate with example why DOM is too "complex" ... compared to SimpleXML. <?php //These are my 2 XML documents. No DTD - just a simple xml structure. $xml1 = '<root1><q/><a><b><c><d>hi1</d></c></b></a><q/></root1>'; $xml2 = '<root2><q/><a><b><c><d>hi2</d></c></b></a><q/></root2>'; $sxe1 = new SimpleXMLElement ($xml1); $sxe2 = new SimpleXMLElement ($xml2); //Easy access to a node. It's great! echo $sxe1->a->b->c->d; echo $sxe2->a->b->c->d; //But how can I understand what is the name of the root node? $dom_sxe = dom_import_simplexml($sxe1); //Hooray, we've got node name echo $dom_sxe->nodeName; //And what happens if element sequence changes? . echo $dom_sxe->childNodes->item(1)->firstChild->firstChild->firstChild->nodeValue; //... hm... I prefer SimpleXML.

Christian Stocker

20 years ago
On 1.11.2005 8:48 Uhr, Marian Kostadinov wrote:
> Who says that I have a well-formed DTD? DTD is not necessary in many > cases. I will illustrate with example why DOM is too "complex" ... > compared to SimpleXML. > > <?php > //These are my 2 XML documents. No DTD - just a simple xml structure. > $xml1 = '<root1><q/><a><b><c><d>hi1</d></c></b></a><q/></root1>'; > $xml2 = '<root2><q/><a><b><c><d>hi2</d></c></b></a><q/></root2>'; > > $sxe1 = new SimpleXMLElement ($xml1); > $sxe2 = new SimpleXMLElement ($xml2); > > //Easy access to a node. It's great! > echo $sxe1->a->b->c->d; > echo $sxe2->a->b->c->d; > > //But how can I understand what is the name of the root node? > $dom_sxe = dom_import_simplexml($sxe1); > > //Hooray, we've got node name > echo $dom_sxe->nodeName; > > //And what happens if element sequence changes? . > echo $dom_sxe->childNodes->item(1)->firstChild->firstChild->firstChild->nodeValue; > //... hm... I prefer SimpleXML.
use xpath :) as easy as simplexml and much more powerfull, IMHO and noone said you should use exclusively dom. chregu
-- christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71 http://www.bitflux.ch | christian.stocker@bitflux.ch | GPG 0x5CE1DECB

Petar Nedyalkov

20 years ago
On Tuesday 01 November 2005 09:48, Marian Kostadinov wrote:
> Who says that I have a well-formed DTD? DTD is not necessary in many > cases. I will illustrate with example why DOM is too "complex" ... > compared to SimpleXML.
Well, If you are free to change the XML structure at any time - where's the point in wondering if an element is a parameter, a CDATA or an element? You can change everything to element and CDATA and that's all. If you have a fixed XML structure, that's not supposed to change, having a DTD is obligatory in orther to maintain consistency of the format. So, you can either work without using getType (since you define the XML structure yourself), or use a more complex API that will serve you well.
> > <?php > //These are my 2 XML documents. No DTD - just a simple xml structure. > $xml1 = '<root1><q/><a><b><c><d>hi1</d></c></b></a><q/></root1>'; > $xml2 = '<root2><q/><a><b><c><d>hi2</d></c></b></a><q/></root2>'; > > $sxe1 = new SimpleXMLElement ($xml1); > $sxe2 = new SimpleXMLElement ($xml2); > > //Easy access to a node. It's great! > echo $sxe1->a->b->c->d; > echo $sxe2->a->b->c->d; > > //But how can I understand what is the name of the root node? > $dom_sxe = dom_import_simplexml($sxe1); > > //Hooray, we've got node name > echo $dom_sxe->nodeName; > > //And what happens if element sequence changes? . > echo > $dom_sxe->childNodes->item(1)->firstChild->firstChild->firstChild->nodeValu >e; //... hm... I prefer SimpleXML.
-- Cyberly yours, Petar Nedyalkov Devoted Orbitel Fan :-) PGP ID: 7AE45436 PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436

Rob Richards

20 years ago
Marian Kostadinov wrote:
>//But how can I understand what is the name of the root node? >$dom_sxe = dom_import_simplexml($sxe1); > >//Hooray, we've got node name >echo $dom_sxe->nodeName; > >//And what happens if element sequence changes? . >echo $dom_sxe->childNodes->item(1)->firstChild->firstChild->firstChild->nodeValue; >//... hm... I prefer SimpleXML. > >
The API is to be kept simple and what you are asking for can be done extending the SimpleXMLElement and the functions are a whole 1 line of user code: <?php class cNode extends SimpleXMLElement { function getName() { return dom_import_simplexml($this)->nodeName; } function getType() { return dom_import_simplexml($this)->nodeType; } } $xml = '<root />'; $sxe = simplexml_load_string($xml, 'cNode'); print $sxe->getName()."\n"; print $sxe->getType()."\n"; ?> Of course XPath could be used in place of DOM too though. Rob