0

the simple example xml file is

<body id="1" doi="100000"> </body> 

php

 $feed = file_get_contents('/../files/schema.xml'); $datasetxml = simplexml_load_string($feed); $datasetxml->body['id']=2; $datasetxml->body['doi']="200000"; echo $datasetxml->asXML(); 

I want to change it as id=2 and doi=200000 but after reset value the result is wrong and only can see in the web page source?

 <?xml version="1.0"?> <body id="1" doi="100000"> <body id="2" doi="200000"/></body> 

2 Answers 2

2

Since it's the root element you want to change, try directly working on it:

$datasetxml = simplexml_load_string('<body id="1" doi="100000"> </body>'); $datasetxml['id']=2; $datasetxml['doi']="200000"; echo $datasetxml->asXML(); 

Because $datasetxml is already the element it's attributes you want to change.

Sign up to request clarification or add additional context in comments.

Comments

1
$feed = file_get_contents('/../files/schema.xml'); $datasetxml = simplexml_load_string($feed); $datasetxml->body[0]['id']=2; $datasetxml->body[0]['doi']="200000"; echo $datasetxml->asXML(); 

1 Comment

This works if you add a root entity to your xml. It seems you can't modify root attribute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.