I have been assigned a project to import data from an atom feed in PHP. I haven't worked too heavily with XML in the past so gave SimpleXML a shot, and quickly found that the word "Simple" in SimpleXML is actively mocking me, notably when it comes to namespaces.
What I've found is that when you attempt to dump data from a part of your XML document that is namespaced, it won't appear, you have to register the namespace first. This becomes a convoluted process when your document looks a little something like this:
<atom:entry>
<atom:title>Example</atom:title>
<xisra:object>
<xis:properties>
<xis:propertyString>Example String</xis:propertyString>
<xis:propertyId>example-id</xis:propertyId>
</xis:properties>
</xisra:object>
</atom:entry>
I began to wonder why I need to register the namespaces at all? What does it mean to "register" a namespace? If the whole XML document is already in memory, why can I not just traverse it in a namespace-agnostic manner like a standard object?
This questioning led me to realize that these namespaces are here for me, the developer, and nobody else. So what benefits am I getting from these namespaces? Are there any real-world scenarios where namespacing like this helps anything? Or more specifically, what benefits are there of requiring me to register each and every namespace to pull the data from an XML document?
The best answer I've found to these questions is essentially that namespacing avoids conflicts in an XML file, but that doesn't really help me understand the benefit of namespacing over simply moving the nodes under a unique name, or requiring registering the namespaces before being able to interact with them.
I concluded that it wouldn't be too difficult to write a namespace-agnostic XML parser, and since that doesn't appear to have been done, I must be missing something vital about the purpose of all of this.
I also found that this isn't only a problem in PHP, but people tend to ask this same question in many languages and the answer is always something along the lines of "you need to register the namespaces that are in the document".
Please help me understand what I'm missing here.