imsop / simplexml_debug Goto Github PK
View Code? Open in Web Editor NEWTools for debugging your code when working with PHP's SimpleXML functions.
License: MIT License
Tools for debugging your code when working with PHP's SimpleXML functions.
License: MIT License
The ability to reference namespaces by their local prefix in SimpleXML is certainly very handy, but I'm increasingly convinced that it shouldn't be relied on. It is perfectly possible for the same file to later be generated using different prefixes for the same namespaces (or even swapping them if they are e.g. "ns1" and "ns2"!).
As this instructional blog post puts it "You’ll have to decide for yourself whether it’s a risk worth taking or not."
simplexml_tree currently outputs access examples using this feature, which could encourage bad practice.
Should an option be provided to choose which style to output? Perhaps change the second parameter ($include_string_content) to be a hash or bit mask controlling various formatting options?
Would be great to see a basis wikipage explaining how to implement your script. Having parsing issues here http://stackoverflow.com/questions/31086403/simplexmlelement-parsing-errors and would love to be able to debug these well. But still more of a frontend than backend guy so could use some tips on how to use your Simple XML debugger.
The xpath()
method doesn't return a SimpleXMLElement
, but an actual array of objects. simplexml_dump()
will currently return an error if you try to pass it this array.
Keeping this limitation keeps the function simple as it always gets a single SimpleXMLElement
object, and doesn't need to do any recursion or similar. However, it means that not every SimpleXML operation can be trivially debugged with this function.
Would need to consider how to handle completely unrelated data types, including arrays with some SimpleXMLElement
values as well as some others:
print_r
or var_export
?The intention of using CC-BY was that it's simple but official.
Some have pointed out, though, that it's not generally a good fit for code, and mixing with other licences.
There seems to be a bug (or misfeature?) in SimpleXML where attributes in the default namespace don't show if you call ->attributes($default_namespace_uri)
rather than ->attributes('', true)
.
Because of simplexml_dump
's attempts to display namespace information, this leads to data disappearing from view with innocuous XML like this:
<?php
include 'simplexml_dump.php';
$xml = <<<XML
<?xml version="1.0"?>
<Foo xmlns="http://example.com">
<Bob InvisibleAttribute="You ain't seen me, right?">
<MaskingChildElement />
</Bob>
</Foo>
XML;
$sx_Foo = simplexml_load_string($xml);
simplexml_dump($sx_Foo->Bob);
While constructing tests for #3, I found a different oddity: currently, I'm relying on ->getNamespaces(false)
returning the namespace of the current element, but that's not actually what it does. Instead, it lists all namespaces used by the current element, including its attributes.
Consider the following:
<?xml version="1.0" standalone="yes"?>
<notinnamespace xmlns:test="http://example.com" test:isinnamespace="true" />
Here, the prefix test
is used in the element (by the attribute) but isn't the namespace to which the element belongs. Since there is no default namespace defined, there's no ''
key in the getNamespaces
output, so the output is [ 'test' => 'http://example.com' ]
, leading to broken dump output.
If the root element of a document is not in the default namespace, but is instead a prefixed node, it is necessary to immediately call ->children() to "select" that namespace. simplexml_tree doesn't currently show this call, unlike other namespace switches.
This is extremely common with SOAP documents, where the root element is the SOAP Envelope, which is not generally set as the default namespace.
See e.g. http://codepad.viper-7.com/eVrQ1u where the root element is "soapEnv:Envelope"
There is currently no way of distinguishing between a single element (e.g. $sxml->Result[0]
) and a list of elements (e.g. $sxml->Result
or $sxml->children()
) which happens to include only one item. They can however behave differently - foreach ( $sxml->Result[0] as $result )
is equivalent to foreach ( $sxml->Result[0]->children() as $result )
.
As far as I can see, there is no way of determining this with the current API. However, since the behaviour is different, the extension itself clearly tracks this distinction. It should probably be raised with the maintainers to add PHP access to this property for a future release.
very nice
It would be nice if its output was more matched to var_dump
datatype(length) value
for example
string(5) "item1"
as opposed to
(string) 'item1' (5 chars)
// var_dump output
object(SimpleXMLElement)#4 (1) {
["item"]=>
string(5) "item1"
}
also if you pass a custom class to it, it is not reflected in output
// simplexml_load_string($xml, 'SimpleXMLExtended')
// var_dump output
object(SimpleXMLExtended)#4 (1) {
["item"]=>
string(5) "item1"
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.