Ive been trying every way possible to create cdata entries in my xml. My latest attempt is as follows. I can't even get passed for the first statement where im creating a new DOMDocument. Any ideas?
<?php
$xml = '
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$xml = simplexml_import_dom($dom);
print "working";
?>
You should not have any characters before the XML declaration. Remove the line break at $xml = '.
The neatest solution would be to use heredoc syntax:
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
XML;
Have a look at: DOMDocument::createCDATASection
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
</cars>
';
$dom = new DOMDocument;
$dom->loadXML($xml);
$cdataNode = $dom->createCDATASection('<&>');
$dom->documentElement->appendChild($cdataNode);
echo $dom->saveXml();
Output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
<make name="Ford">
<model>Mustang</model>
</make>
<make name="Honda">
<model>Accord</model>
</make>
<![CDATA[<&>]]></cars>
Related
Considering this XML
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0">
<Subject>
<NameID>801234567890</NameID>
</Subject>
....
</Assertion>
PHP
$dom = new DOMDocument();
$ret = $dom->loadXML($data);
$xp = new DOMXPath($dom);
$node_list = $xp->query('/Assertion');
$node_list->length return 0 element. I want to extract the DOMElement but somehow it didn't work.
As stated on the comments by Sami Kuhmonen you may need to register the namespaces, here is an example:
<?php
$string= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:SAML:2.0:assertion http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd" ID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc" IssueInstant="2009-09-09T00:46:02Z" Version="2.0">
<Subject>
<NameID>801234567890</NameID>
</Subject>
</Assertion>
XML;
$dom = new DOMDocument();
$dom->loadXML($string);
$xp = new DOMXPath($dom);
// registering the namespaces
$xp->registerNamespace('a','urn:oasis:names:tc:SAML:2.0:assertion');
$xp->registerNamespace('b','http://www.w3.org/2001/XMLSchema-instance');
// using the prefix of the registered namespace in the xpath expression
$node_list = $xp->query('/a:Assertion');
print $node_list->length
?>
I try to add a single element/single tag without a closing tag to an XML structure with PHP Dom to get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml>
<credentials>
<identity>User</identity>
<password>Password</password>
<language name="default" modifier="de"/>
</credentials>
</ndxml>
Does anybody know which command I can use to get this?
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $domDocument->createElement('ndxml');
$credentials = $domDocument->createElement('credentials');
$credentials->appendChild(new \DOMElement('identity', 'User'));
$credentials->appendChild(new \DOMElement('password', 'Password'));
$language = $domDocument->createElement('language');
$language->setAttribute('name', 'default');
$language->setAttribute('modifier', 'de');
$credentials->appendChild($language);
$rootNode->appendChild($credentials);
$domDocument->appendChild($rootNode);
echo $domDocument->saveXML();
echo "\n\n";
echo $domDocument->saveXML(null, LIBXML_NOEMPTYTAG);
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"/></credentials></ndxml>
<?xml version="1.0" encoding="UTF-8"?>
<ndxml><credentials><identity>User</identity><password>Password</password><language name="default" modifier="de"></language></credentials></ndxml>
I have XML of the form
$xml=<<<EOD
<?xml version="1.0" encoding="utf-8"?>
<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>
</RequesterCredentials>
<ItemID>ItemIDVALUE</ItemID>
<TransactionID>TransactionIDVALUE</TransactionID>
<Shipped>1</Shipped>
<Paid>1</Paid>
<Shipment>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>
</Shipment>
<ErrorLanguage> string </ErrorLanguage>
EOD;
I want to append more ShipmentTrackingDetails portion using PHP, I need the resulting XML like this:
$xml=<<<EOD
<?xml version="1.0" encoding="utf-8"?>
<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>
</RequesterCredentials>
<ItemID>ItemIDVALUE</ItemID>
<TransactionID>TransactionIDVALUE</TransactionID>
<Shipped>1</Shipped>
<Paid>1</Paid>
<Shipment>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo2</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>$trackingNo3</ShipmentTrackingNumber>
<ShippingCarrierUsed>UPS</ShippingCarrierUsed>
</ShipmentTrackingDetails>
<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>
</Shipment>
<ErrorLanguage> string </ErrorLanguage>
EOD;
PHP Source:
$sxe = new SimpleXMLElement($xml);
$track = $sxe->addChild('ShipmentTrackingDetails');
$track->addChild("ShipmentTrackingNumber", "9876");
$track->addChild("ShippingCarrierUsed", "USPS");
I am not getting how to do this using php.
Please help in getting this working.
Thanks!
I got it,
The dynamic part would be:
$sxe = new SimpleXMLElement($xml);
$TrackingDetails = $sxe->Shipment->addChild('ShipmentTrackingDetails');
$TrackingDetails->addChild("ShipmentTrackingNumber", $trackingMore);
$TrackingDetails->addChild("ShippingCarrierUsed", "UPS");
$sxe->asXML("tracking.xml");
$xml1 = $sxe->saveXML();
Where $xml is the original xml to which we want to add new child.
Trying to write clean XML for a QuickBooks integration using DOMDocument in PHP. The only thing I am stuck on is how to add the required <?qbxml version="2.0"?> after <?xml version="1.0" encoding="utf-8"?> to produce the following:
<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="2.0"?>
.....
Here is what I have so far..
$dom = new DOMDocument('1.0', 'utf-8');
//Need to somehow add qbxml version here
$qbxml = $dom->appendChild($dom->createElement('QBXML'));
$qbxmlmsg = $qbxml->appendChild($dom->createElement('QBXMLMsgsRq'));
$qbxmlmsg->setAttribute('onError', 'stopOnError');
$salesReceiptAddRq = $qbxmlmsg->appendChild($dom->createElement('SalesReceiptAddRq'));
$salesReceiptAddRq->setAttribute('requestID', 1234);
$dom->formatOutput = true;
echo $dom->saveXML();
That node is called a processing instruction.
$dom = new DOMDocument('1.0', 'utf-8');
$dom->appendChild($dom->createProcessingInstruction('qbxml', 'version="2.0"'));
$qbxml = $dom->appendChild($dom->createElement('QBXML'));
// ...
echo $dom->saveXml();
I'm trying to add a childnode in an XML document with PHP and got it OK so far except one thing. Can't get it formatted correct?
Here is the script:
$xmldoc = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xmldoc->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<udate>1347730639</udate>
<userid>3</userid>
</data>');
echo "<xmp>OLD:\n". $xmldoc->saveXML() ."</xmp>";
$root = $xmldoc->firstChild;
$newElement = $xmldoc->createElement('popup');
$root->appendChild($newElement);
$newText = $xmldoc->createTextNode("0");
$newElement->appendChild($newText);
echo "<xmp>NEW:\n". $xmldoc->saveXML() ."</xmp>";
After adding the node I get this:
<data>
<udate>1347730639</udate>
<userid>3</userid>
<popup>0</popup></data>
I want it to be like this:
<data>
<udate>1347730639</udate>
<userid>3</userid>
<popup>0</popup>
</data>
Where do I go wrong?
Please help and thanks in advance :-)
createElement will break formatOutput this is a general issue
See PHP BUG Report
formatOutput does not work with saveHTML
DOMDocument->formatOutput = true does not work
But you can have a work around by reloading and formatting it.
$xmldoc = new DOMDocument();
$xmldoc->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<udate>1347730639</udate>
<userid>3</userid>
</data>');
echo "<xmp>OLD:\n". $xmldoc->saveXML() ."</xmp>";
$root = $xmldoc->firstChild;
$newElement = $xmldoc->createElement('popup');
$root->appendChild($newElement);
$newText = $xmldoc->createTextNode("0");
$newElement->appendChild($newText);
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML( $xmldoc->saveXML());
echo "<xmp>NEW:\n". $xml->saveXML()."</xmp>";
Output
OLD:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<udate>1347730639</udate>
<userid>3</userid>
</data>
NEW:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<udate>1347730639</udate>
<userid>3</userid>
<popup>0</popup>
</data>
You might want to try removing all whitespace before and then formatting.
force it to remake the xml from scratch.
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;