Out of the difficulty in using SimpleXML (when it involves namespaces and cdata.) I have created this class using the PHP DOM functions.
This class provides a search function that actually outputs the php statements required to access a certain node....
After loading the XML string, just call the `search` or `dump` function with the name of a node, and the class will print out the nodes and the php statements to be used to access that node, no matter what namespaces they are in.
For example Consider the following (complex) xml fragment
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2005-10-28T01:01:04.668Z</Timestamp>
<Ack>Success</Ack>
<Version>429</Version>
<Build>e429_intl_Bundled_1949355_R1</Build>
</GeteBayOfficialTimeResponse>
</soapenv:Body>
</soapenv:Envelope>
Code: Select all
print_r($srcXML->search('Timestamp'));Array
(
[0] => Array
(
[nodeName] => Timestamp
[namespaceURI] => urn:ebay:apis:eBLBaseComponents
[nodeValue] => 2005-10-28T01:01:04.668Z
[accessStatement] => ...->{'soapenv:Envelope'}->{'soapenv:Body'}->{'urn:ebay:apis:eBLBaseComponents|GeteBayOfficialTimeResponse'}->{'urn:ebay:apis:eBLBaseComponents|Timestamp'}
)
)
Here accessElement contains
Code: Select all
...->{'soapenv:Envelope'}->{'soapenv:Body'}->{'urn:ebay:apis:eBLBaseComponents|GeteBayOfficialTimeResponse'}->{'urn:ebay:apis:eBLBaseComponents|Timestamp'}Code: Select all
echo $srcXML->{'soapenv:Envelope'}->{'soapenv:Body'}->{'urn:ebay:apis:eBLBaseComponents|GeteBayOfficialTimeResponse'}->{'urn:ebay:apis:eBLBaseComponents|Timestamp'} You can search namespaced nodes too using the search function. just use the nodename with prefix as it appears in XML.
for Eg:
Code: Select all
$srcXML->search('soapenv:Body');Its also super easy to generate XML with this class. Please go through the following examples, or the detailed documentation at
http://crxml.pagodabox.com
or
http://www.phpclasses.org/browse/file/34394.html
You can download the class, documentation and demo script from
http://crxml.pagodabox.com/crxml.tar
To illustrate the XML generation capabilities of this class, I have put an online self guided tutorial at
http://crxml.pagodabox.com/demo.php
The functionality of this class is similar to simpleXML,It implements ALL the functionality of simpleXML and much more.
it differs from simpleXML in the way in which namespaced nodes are addressed. In simpleXML namespaced nodes cannot be addressed directly.
In this class a namespaced node can be addressed directly as
$crxmlObject->{'prefix:nodeName'}
If the node defines a default namespace,ie a namespaceURL with out a prefix, then that node can be addressed as
$crxmlObject->{'http:namespaceURI|nodeName'}
this syntax can be used when generating and also while parsing an XML document.(Please see the example below )
A cdata node can be added using the syntax
$crxmlObject->nodeName = (object) "this is the content of a CDATA node" ;
This makes working with namespaces and CDATA sections a little more convenient, or rather, straightforward ( I hope ).This applies to both generation and xml parsing.It also makes it easy to manipulate existing XML documents. Like copying one node from one XML to another or deleting a node or appending a node to another nodes.
Regarding xml manipulaton, All the xml manipulation can be done using the normal assignment operator and four methods $node->appendTo, $node->appendNode, $node->replaceWith and $node->appendChildren. In the posts below you can see various examples of usage of these functions.
This class supports:-
1. XML Attributes
2. XML Namespaces
3. XML Default Namespaces
3. XML Namespaced attributes
4. CDATA sections
please take a look at this example code which illustrate a simple generation of a namespaced xml string,
Code: Select all
<?php
$xmlOne=new crXml();
$xmlOne->root->{'http://google.com|person|prfx'} = 'sandeep';
echo $xmlOne->xml();
?><?xml version="1.0" encoding="UTF-8"?>
<root>
<prfx:person xmlns:prfx="http://google.com">
Sandeep
</prfx:person>
</root>
Here is the link to the class file
http://crxml.pagodabox.com/crXml.tar
or
http://www.phpclasses.org/browse/downlo ... /crXml.php
Here is a detailed documentation about its usage.
http://crxml.pagodabox.com
I am adding a LIVE DEMO so that people can check the class out with out downloading anything. Here is the link
http://crxml.pagodabox.com/demo.php
Please, Please do let me know of your comments.
Regards,
Sandeep/Max.