I have a question regarding addChild in SimpleXMLElement.
i try to create xml file using SimpleXMLElement with specific header as a requirement and structure like this :
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<ns1:Order xmlns:ns1=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\"><node id=\"1\" name=\"node 1\"><subnode id=\"1.1\" name=\"subnode 1.1\"/><subnode id=\"1.2\" name=\"subnode 1.2\"><inner_node id=\"1.2.1\" name=\"inner node 1.2.1\"/></subnode></node></ns1:Order>
this is my code :
$xml_header = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns1:Order xmlns:ns1="rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd"></ns1:Order>';
$xml = new SimpleXMLElement($xml_header);
$node1 = $xml->addChild('node');
$node1->addAttribute('id', '1');
$node1->addAttribute('name', 'node 1');
$subnode1 = $node1->addChild('subnode');
$subnode1->addAttribute('id', '1.1');
$subnode1->addAttribute('name', 'subnode 1.1');
$subnode2 = $node1->addchild('subnode');
$subnode2->addAttribute('id', '1.2');
$subnode2->addAttribute('name', 'subnode 1.2');
$inner_node1 = $subnode2->addChild('inner_node');
$inner_node1->addAttribute('id', '1.2.1');
$inner_node1->addAttribute('name', 'inner node 1.2.1');
$this->response = $xml->asXML();
output:
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<ns1:Order xmlns:ns1=\"rrn:org.xcbl:schemas/xcbl/v3_5/xcbl35.xsd\"><ns1:node id=\"1\" name=\"node 1\"><ns1:subnode id=\"1.1\" name=\"subnode 1.1\"/><ns1:subnode id=\"1.2\" name=\"subnode 1.2\"><ns1:inner_node id=\"1.2.1\" name=\"inner node 1.2.1\"/></ns1:subnode></ns1:node></ns1:Order>
Does anyone have any idea how to adding a child without ns1 prefix inside ns1:Order tag?
Related
How can i get the content of an XML as string?
I have a xml like this:
<?xml version="1.0" encoding="utf-8"?>
<section>
<paragraph>some content</paragraph>
<custom>some more content</custom>
</section>
and i want the content of section as a string like this:
<paragraph>some content</paragraph>
<custom>some more content</custom>
every doc i read so far only explains how to get the content of a subnode but not of the root node.
It can be achieved using DOMDocument (Press here to See it in action)
$xmlStr = '<?xml version="1.0" encoding="utf-8"?>
<section>
<paragraph>some content</paragraph>
<custom>some more content</custom>
</section>
';
$xml = new DOMDocument();
$xml->loadXML($xmlStr);
if($xml->childNodes->length > 0) {
foreach($xml->childNodes->item(0)->childNodes as $rootChildNode){
echo $xml->saveXML($rootChildNode);
}
}
You can use xml parser for create an array and work with it. For example:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<section>
<paragraph>some content</paragraph>
<custom>some more content</custom>
</section>
XML;
$values = [];
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $xml, $values);
xml_parser_free($p);
var_dump($values);
http://php.net/manual/en/ref.xml.php
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>
According to this answer it is possible to echo out formatted xml. Yet this php code:
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><data></data>");
$xml->addChild("child1", "value1");
$xml->addChild("child2", "value2");
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
outputs value1 value2
So how do I format it correctly nowadays?
To echo out formatted XML (or HTML) you have to use htmlentities built-in function, that “convert all applicable characters to HTML entities”.
In your case:
echo htmlentities($dom->saveXML());
will output this:
<?xml version="1.0" encoding="utf-8"?> <data> <child1>value1</child1> <child2>value2</child2> </data>
Using-it together with <pre> html tag, also newlines and spaces will be printed:
echo '<pre>' . htmlentities($dom->saveXML()) . '</pre>';
will output this:
<?xml version="1.0" encoding="utf-8"?>
<data>
<child1>value1</child1>
<child2>value2</child2>
</data>
I have the following code
$base = '<?xml version="1.0" encoding="UTF-8"?><realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink"></realestates:office>';
$objXml = new \SimpleXMLElement($base);
$objXml->addChild('title', 'Alles Toller');
$strXml = $objXml->asXML();
$strXml would now be
<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<realestates:title>Alles Toller</realestates:title>
</realestates:office>
What I want is to not have the realestates: prefix in <title>
<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Alles Toller</title>
</realestates:office>
How can I accomplish that?
I ended up in using DOMDocument:
$objXml = new \DOMDocument( "1.0", "UTF-8" );
$objRoot = $objXml->createElement('realestates:office');
$objXml->appendChild($objRoot);
$objXml->createAttributeNS('http://rest.immobilienscout24.de/schema/offer/realestates/1.0', 'realestates:office');
$objXml->createAttributeNS('http://www.w3.org/1999/xlink', 'xlink:dummy');
$objTitle = $objXml->createElement('title', 'Alles Toll');
$objRoot->appendChild($objTitle);
$strXml = $objXml->saveXML();
The second parameter of createAttributeNS() is a bit strange - only the namespace is not enough, it looks like I have to also add an element name.
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>