This post has been moved to my technical blog.
Direct link : http://abstraction.blog/2010/09/04/php-xml-create-add-edit-modify-using-dom-simplexml-xpath
This post has been moved to my technical blog.
Direct link : http://abstraction.blog/2010/09/04/php-xml-create-add-edit-modify-using-dom-simplexml-xpath
Example 9 doesn’t work for me, it outputs:
Ayn Rand
300
J.R.R.Tolkein
500
Stephen King
200
It appears that the changes aren’t updated.
Good Catch.
DOM Code Rectified so as to reflect appropriate XML indices.
SimpleXML code changed so as to depend on DOM now since earlier code wouldn’t work for CDATA section.
Thanks this works perfectly, where did you find the information about DOM’s XML indices? I would like to read up on that.
I’ve been looking to do reordering of XML elements on and off for 4 years now. Thanks again!
Discovered it quite a while back when I was practicing with XML DOM. Index 0 is tied to a blank text node while Index 1 points to the XML node (1st book) & so on alternatively. Had forgotten abt it since SimpleXML doesn’t have it.
Thank you for taking time to write down this write-up. It’s been rather useful. It couldn’t have come at a superior time for me!
Thanks dude. This page saved me from pulling my hair out with SimpleXML – switched to DOM XML and it was much easier!
You are welcome. I can understand your situation…went through the same last year…hence decided to do something about it. Glad it was of use to you 🙂
Great examples ! That’s what I was looking for. Just one thing I’m struggling with, maybe you can help. My xml structure is hierarchical :
http://pastebin.com/zGKShpgS .
I need to find element by id so i use xpath->query(‘//item[@id=x]) , but now how can I appendChild after or before this element ? I’d thought it would be as simple as $library->insertBefore($newitem,$xpathresult), but it doesn’t seem to work. Any ideas ?
have anyone a working example? i need add/delete/edit entries in xml file… THX
As far as I know, all the above examples work 😐
Hi there I got question:
I am trying to edit below table.xml file using php base on the result.xml but your solution did not work for me. can you help me please.
table.xml
Man U
1
0
0
1
0
0
Chelsea
0
0
0
0
0
o
LiverPool
1
1
0
0
0
3
Arsenal
1
0
1
0
0
1
and result.xml
Results
Man u
6
Chelsea
0
.
.
.
thanks
Sorry my result.xml file struct:
Man u
6
Chelsea
0
.
.
.
Here is the details what exactly I am trying to do:
read the result.xml file
then compare with team name with table.xml
if team name match with result.xml
update record in table.xml
for home team
update no of match played +1
update points on the base of goal (6-0)
for away team
update no of match played +1
update points on the base of goal (0-6)
hope you got it now
Not sure if I understood your xml structure…cause the numbers don’t make sense…do they represent goals scored or points achieved ? why don’t you mention the proper xml with the node names instead of just the values.
As far as I can understand, you plan to use the result.xml file to update the table.xml file using the club name as the search parameter. The “(3) Edit XML – Edit specific Elements (accessed conditionally)” eg should help you to find the club name (assuming its an attribute value).
how can i post proper xml file here, coz i cant post xml structure here.
<
Man U
1
0
0
1
0
0
Chelsea
0
0
0
0
0
o
LiverPool
1
1
0
0
0
3
Arsenal
1
0
1
0
0
1
>
Place your xml code inside the WordPress sourcecode tags (http://en.support.wordpress.com/code/posting-source-code/) & give the language as xml.
eg :
<?xml version="1.0"?><library><book isbn="1001" pubdate="1943-01-01"><title><![CDATA[The Fountainhead]]></title><author>Ayn Rand</author><price>300</price></book></library>
here is my xml file
table.xml
Man U
1
0
0
1
0
0
Chelsea
0
0
0
0
0
o
LiverPool
1
1
0
0
0
3
Arsenal
1
0
1
0
0
1
result.xml
Man U
6
Chelsea
0
.
.
.
.
.
.
hope you will be able to help.
result.xml file:
table.xml
hope you will be able to help.
Sorry I couldn’t reply earlier….bunch of stuff kept me busy.
Built a sample solution in which we read from result.xml & edit the played element of table.xml :
Thank you very much for your help, but function did not work with me, sorry to bother you again, have you tested the function.? Hope you will get back to me when you got time. thanks.
Here is the error:
Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\dev\www\read.php on line 17
which is pointing last line of if statement, can you please have a look. thanks
hi there, i have modified your code and end the for loop before if statement becouse loop goes round 3 time with the tags (, and ) as result played add up 3 times. it work fine but i cant read more then one result, can you please have a look the code i have modified?? thank you in advance.
Nice tutorial. Thanks!
All sorted thanks for your help Rohit, you are great man. keep it up. thank you.
Was going to check your code tonight….but good that you worked it out. You are welcome !
Hi, I am looking for something similar, its part of my assignment in college. Could you please help me if possible. It would be great help. I would immensely appreciate if you could mail it to me. My email id is sanjyot_31@yahoo.com
hi there, i was try to learn about php and xml but when i tried to cek, update, and create i got stuck, maybe you can help me out with this snippet
load(‘server.xml’);
$dashboard_xpath = new DOMXPath($dashboard_xml);
$dashboard_query = $dashboard_xpath->query(‘/Dashboard/Server[@id=1]’);
$dashboard_result = false;
foreach($dashboard_query as $dashboard_result){
if($dashboard_result){
// Update XML FIle Elements;
} else {
// Create XML Elements;
}
}
?>
172.17.2.71
80
3306
172.17.2.71
21
80
i was googling all day long but still nothing, wish you can help me out with a bit example or direct we to a good tuts web, really thanks before.
Hi,
dom_import_simplexml is not working when i tried for Reorder of elements in XML.
I am getting empty array.
Let me know what will be the problem.
$libSimple= new SimpleXMLElement(‘library.xml’,null,true); // getting Simplexmlelement
$libDom = dom_import_simplexml($libSimple); // Empty array is the output for this line.
Thanks,
Prakash Mukta
dom_import_simplexml is an inbuilt function.
It will always work provided the xml file is proper.
Btw thx to your comment, I just checked the code & found an issue in the reordering of elements when the elements are not alongside each other.
The issue is fixed now !
Good afternoon,
How would I do if my xml was cascading?
eg
Hello, I am trying to append an XML file and i should save new elements to the xml file, information taken from textboxes. But unfortunately after trying some of your codes i am unable to write the my xml file.
Could you please help.
Wow – this post has saved me from giving up on XML – after hours of searching, failing, etc. This post is AMAZING! Thank you!
Hi Thank you for this tutorial, soo so helpful,
The code is working fine on the browser but not saving the changes to the file and no thing change in the xml file?
(2) Edit XML – Edit/Modify Element Data (accessed serially)
part (ii) DOM – not working for me.
$cnt = $library->childNodes->length; // $cnt return 7 !??? not 2 – it is 3 books
$library->childNodes->item($cnt-1)->getElementsByTagName(‘title’)->item(0)->nodeValue .= ‘ Series’;
== Fatal error: Call to undefined method DOMText::getElementsByTagName() …
foreach($library->childNodes as $node){
echo $node->nodeName.’ – ‘.$node->nodeValue, “”;
}
// it is list 7 childrens & values 😦
/*
#text –
book – The Fountainhead Ayn Rand 300
#text –
book – The Lord of the Rings J.R.R.Tolkein 500
#text –
book – The Dark Tower Stephen King 200
#text –
now is ok 🙂 i eliminated return (eol) from xml
can someone post examples in files ?
That’s great work. But, what about if I want to add children to the last node in the XML file, how can I do that ?
Hello congratulations for your article.
I used the codes, I created a file library.xml and lib.php where I copied the inside:
// (I) SimpleXML
// Edit Last Book Title
fnSimpleXMLEditElementSeq function ()
{
$ library = new SimpleXMLElement (‘library.xml’, null, true);
$ num = count ($ library);
$ library-> book [$ num-1] -> title. = ‘- The Gunslinger’;
header (“Content-type: text / xml”);
echo $ library-> asXml ();
}
But unfortunately it does not work, where am I wrong? as the function is called or what parameters I have to pass?
Thank you and congratulations for the work done.
Very nice article, helped me a lot
Tank you
I think for append record before you have wrong code as you have written
header(“Content-type: text/xml”);
echo $dom->saveXML();
Instead of
echo $dom->saveXML(); it should be
echo $dom->save(‘library.xml’);