Skip to content

Instantly share code, notes, and snippets.

@cspray
Created May 2, 2012 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspray/2577396 to your computer and use it in GitHub Desktop.
Save cspray/2577396 to your computer and use it in GitHub Desktop.
Why we don't like XML

I was recently working with the FogBugz XML API and I needed to get some attribute value from the response. Below is the perfectly legal call chain needed to get an attribute value from my preferred XML parser, DOMDocument:

$case_id = $dom_document->getElementsByTagName('case')->item(0)->attributes->getNamedItem('ixBug')->nodeValue;

Assuming FogCreek chose to go with a JSON API for FogBugz we could expect something along these lines to get the same value:

$case_id = $json->case->ixBug

For this reason I would prefer to work with a JSON API as compared to an XML API. Imagine the code you'd need to actually parse a complete XML document and not simply returning an attribute value.

@edorian
Copy link

edorian commented May 2, 2012

Did you mean?

 $case_id = $dom_document->getElementsByTagName('case')->item(0)->getAttribute('ixBug');

Else I'm not really sure why you go to attributes here as both things imply different meaning :)

@cspray
Copy link
Author

cspray commented May 2, 2012

I don't see a getAttribute() method in the DOMNode object. Did I miss something?!

@edorian
Copy link

edorian commented May 2, 2012

Are you getting a DomNode or a DOMElement there? I'm quite sure you should be getting the latter

@cspray
Copy link
Author

cspray commented May 2, 2012

DOMNodeList->item() says it returns a DOMNode. Now, I must go do some checking...off to the editor!

@edorian
Copy link

edorian commented May 2, 2012

php -r '$x = new DomDocument(); $x->loadXml("<foo><bar baz=\"7\"/></foo>"); $z = $x->getElementsByTagName("bar"); var_dump($z->item(0));'
class DOMElement#3 (0) {
}

It returns "at least" a DOMNode but in the cases where the entries are DomElements it returns the real thing.

@cspray
Copy link
Author

cspray commented May 2, 2012

Indeed. Just verified this myself. I wonder how it decides whether to return a DOMNode or DOMElement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment