2

Try to wright a xml with SimpleXmL. Need to nest some of the tags

 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><orders></orders>'); $orderlist = $xml->addChild('order'); $orderlist->addAttribute('shipdate', date('Y-m-d H:i:s')); $orderlist = $xml->addChild('delivery'); $orderlist->addAttribute('weight', '0'); 

This will output this:

 <?xml version="1.0" encoding="utf-8"?> <orders> <order shipdate="2017-10-11 13:44:12" /> <delivery weight="0" /> <order/> </orders> 

But need the tag not to close before the tag. Want it to look like this:

<?xml version="1.0" encoding="UTF-8"?> <orders> <order shipdate="2016-07-14T14:41:30"> <delivery weight="0" /> </order> </orders> 

2 Answers 2

3

Try the following:

 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><orders></orders>'); $orderlist = $xml->addChild('order'); $orderlist->addAttribute('shipdate', date('Y-m-d H:i:s')); $delivery = $orderlist->addChild('delivery'); $delivery->addAttribute('weight', '0'); 

This will add the <delivery> tag inside <order> tag. And provide the output like:

<?xml version="1.0" encoding="utf-8"?> <orders> <order shipdate="2017-10-11 22:28:13"> <delivery weight="0"/> </order> </orders> 
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to add child to $orderlist, then write it properly:

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><orders></orders>'); // add child to root node `orders` $orderlist = $xml->addChild('order'); $orderlist->addAttribute('shipdate', date('Y-m-d H:i:s')); // add child to newly created `order` node $delivery = $orderlist->addChild('delivery'); $delivery->addAttribute('weight', '0'); echo $xml->asXML(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.