Adding Child element to XML - php

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.

Related

Add single element/single tag to XML structure with PHP Dom

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>

SimpleXML doesn't show the value

I am wondering why the below code doesn't show the value of processResponse tag while it shows the whole XML and the Body tag.
This is the XML which I am handling
$xml = '<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:MessageID>urn:df1231asfer5e4564affds</wsa:MessageID>
<wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
</env:Header>
<env:Body>
<processResponse xmlns="http://xmlns.oracle.com/EligibilityProcess/EligibilityProcess/EligibilityBPEL">
<generatedMessageRefNo>451</generatedMessageRefNo>
<providerRefNo>41</providerRefNo>
<tpaRequestId>4184612387</tpaRequestId>
<contractHolder>Rami Zbeeb</contractHolder>
<contractNo>81456954</contractNo>
<guarantorName>ANC</guarantorName>
<eligibilityStatus>Success</eligibilityStatus>
<eligibilityReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<messageOrNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<patientShare></patientShare>
<consentForm></consentForm>
<webServTechStatus>Success</webServTechStatus>
<replyCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<replyDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</processResponse></env:Body></env:Envelope>';
I am using the SimpleXML class:
$res = new SimpleXMLElement($xml);
When I am showing the body XML it works:
$str = $res->children('env',true)->Body->asXML();
echo "<pre>",htmlentities($str),"</pre>";
However when showing the processResponse XML or string it doesn't work:
$str = $res->children('env',true)->Body->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
Kindly advice.
You can get the children of Body to get the processResponse:
$str = $res->children('env',true)->Body->children()->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";

Can't create element in Xml file with Php

I'm working on some project and i have to save movie(id,title,genre) in a xml file using php.
when i create xml file and add information it works perfectly
I mean ,
file : php
$document_xml = new DomDocument("1.0","UTF-8");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->save("data.xml");
and i have
file : data.xml
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
but when i modify the code like this
file : php
$document_xml = new DomDocument();
$document_xml->load("data.xml");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->saveXML();
so that to have,
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>id</id>
<title>some title</title>
<genre>some genre</genre>
</movie>
I have no error and there is no change in my xml file
I would like your help
I found solution, i change the structure of my xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>2</id>
<title>La guerre des etoiles</title>
<genre>adventure</genre>
</movie>
<movie>
<id>3</id>
<title>Jugement dernier</title>
<genre>action</genre>
</movie>
</data>
i change my php file like this
$document_xml = new DomDocument();
$document_xml->load("data.xml",LIBXML_NOBLANKS);
$document_xml->formatOutput = true;
$root = $document_xml->documentElement;
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$value);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$root->appendChild($movie_dom);
$document_xml->appendChild($root);
$document_xml->save("data.xml");
everything wworks perfectfly ...

XML Parsing error

I trying to parse XML file but getting parsing error.
Code ::
$xmlUrl = 'products.xml';
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
XML file ::
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp's Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
I am getting error because of this tag
<client_name>Knapp's Donut Shop</client_name>
The conversion to a SimpleXML Object and the output works, see code example below.
Check your "products.xml" file for the correct UTF-8 encoding type.
<?php
$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp & Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
XML;
$xml = str_replace(array("&", "&"), array("&", "&"), $xml);
$xmlObj = simplexml_load_string($xml);
var_dump($xmlObj);
echo PHP_EOL . $xmlObj->orderlist->client_name;
// Result: Knapp & Donut Shop

SimpleXML PHP - Why DOM functions don't work, cdata trouble

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>

Categories