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 ...
Related
I want to get attribut value type (ss:Name) from xml element with php.
But it doesn't work.
This is my xml file :
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Worksheet ss:Name="097-097-024">
</Worksheet>
</Workbook>
and my php source
<?php
$xml = simplexml_load_file($fichier);
$attr = $xml->Worksheet->Workbook->attributes();
echo $attr['ss:Name'];
?>
Can you help my to get ss:Name value ?
Thanks
The prefix of your attribute ss is a namespace.
You can grab attributes for that namespace by requesting it as the argument for Attributes() like this:
$node->Attributes('urn:schemas-microsoft-com:office:spreadsheet');
$xml = '<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Worksheet ss:Name="097-097-024">
</Worksheet>
</Workbook>';
Example php:
<?php
$xml = simplexml_load_string($xml);
foreach($xml->Worksheet->Attributes('urn:schemas-microsoft-com:office:spreadsheet') as $key=>$val) {
echo "$key: $val\n";
}
outputs
Name: 097-097-024
also note that your top node is Worksheet not Worksheet->Workbook
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.
I have a small requirement where I need to create a XML file on the fly. It was no problem for me to create a normal xml file which would be looking like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name></name>
</item>
</root>
But my requirement is such that I need to create a XML file whose output is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name url = "C:\htdocs\proj1\source_file1"/>
<name url = "C:\htdocs\proj1\source_file2"/>
<name url = "C:\htdocs\proj1\source_file3"/>
</item>
</root>
I have tried in this fashion:
<?php
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$xmlRoot = $domtree->createElement("root");
$xmlRoot = $domtree->appendChild($xmlRoot);
$item = $domtree->createElement("item");
$item = $xmlRoot->appendChild($item);
$name= $domtree->createElement("name");
$name = $item->appendChild($name);
$sav_xml = $domtree->saveXML();
$handle = fopen("new.xml", "w");
fwrite($handle, $sav_xml);
fclose($handle);
?>
But I wanted to append/add the url="path" to my elements. I have tried declaring variables with url and path but this throws me errors like:
Uncaught exception 'DOMException' with message 'Invalid Character Error'
Any ideas how to approach this problem!
Thanks
You just have to declare that attributes via php DOM:
...
$name= $domtree->createElement("name");
$urlAttribute = $domtree->createAttribute('url');
$urlAttribute->value = 'C:\htdocs\proj1\source_file1';
$name->appendChild($urlAttribute);
$item->appendChild($name);
...
Link to DOMDocument docs
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>