Editing and deleting XML nodes with PHP - php

I've been looking around but still can't find a workable answer to editing and deleting xml with php without the use of simpleXML (how I wish i could use it). I was hoping someone could break it down simply for me, as my brain doesn't cope with this stuff! The xml file I have to work with looks something like this:
<allentries>
<entry>
<entryid>1</entryid>
<title>This is the title</title>
<date>2010</date>
<author>Some Guy</author>
</entry>
<entry>
<entryid>2</entryid>
<title>This is Another title</title>
<date>2011</date>
<author>Some Other Guy</author>
</entry>
<entry>
<entryid>3</entryid>
<title>This is the other title</title>
<date>2012</date>
<author>And Another Guy</author>
</entry>
</allentries>
I need to be able to do two things with PHP - Firstly I need to be able to accept values posted from a form, pick the correct entry based on the entryid number and modify each element within that entry with the new data. For eg. if entryid = 2, skip to the second entry and replace the text within title, date and author with the new data.
The second thing I need to be able to do (with a different function obviously) is pick the entry based on the entryid number and delete the whole entry, tag, data, children and all.
It sounds like it shouldn't be too hard, but every example I've used so far fails to do anything. Can anybody suggest anything or any other tutorials somewhere that may help me?
Thanks muchly for your time!

2 quick ways, both using regular expressions:
break the XML to array using regular expressions, with the value of the entryid as key
on update just construct the new value and replace the value at the specified key
on delete just unset
use regular expression to replace nodes
No idea on the performance for this two cases. I'll try and provide an regular expression to help you.
Gabriel

Ok, sorry, did some further research and found a good example at this site using:
$xml_file = "thefile.xml";
if(!$xml=simplexml_load_file($xml_file)){
trigger_error('Error reading XML file',E_USER_ERROR);
}
simplexml_load_file instead of simplexml_load_string works like a charm now! No wonder you guys were confused - thanks for all your replies and for tolerating my noobishness!

Related

Map XML accordingly

I am having trouble finding a solution to a problem I am facing, parsing XMLs.
Let me describe what I have now and what's the issue:
I have LINKs of XMLs files that have for example:
<prodcuts>
..
<product>
<id>1</id>
<name><![CDATA[ this is a test product name ]]></name>
<link><![CDATA[http://www.google.com]]></link>
<image><![CDATA[http://www.google.com/image.jpg]]></image>
<sku><![CDATA[ ]]></sku>
<category><![CDATA[ System > Technology ]]></category>
<price>20</price>
<description><![CDATA[ ]]></description>
<instock><![CDATA[ Y ]]></instock>
<availability>Y</availability>
</product>
..
</products>
Another XML has:
<prodcuts>
..
<product>
<productID>1</productID>
<title><![CDATA[ ]]></title>
<link><![CDATA[http://www.google.com]]></link>
<image><![CDATA[http://www.google.com/image.jpg]]></image>
<sku><![CDATA[ ]]></sku>
<categoryPath><![CDATA[ System > Technology ]]></categoryPath>
<price>20</price>
<description><![CDATA[ ]]></description>
<instock><![CDATA[ Y ]]></instock>
<availability>Y</availability>
<size>40</size>
</product>
..
</products>
Now, the difference between those are
1) the first one has a tag name "name", the other one has a tag name "title".
2) The second one has some tags that the first one does not.
Now the problem is, I am parsing the XML file via PHP like this:
$xml->products->product[$i]->id
$xml->products->product[$i]->name
and so on.. If I do this the code I have wrote, will work only for the first one. The tags that are missing is not a problem for now, cause I am inserting to Database NULL cause there are not required fields..
But, what about the second XML? Can I do something "automatically" in order to avoid asking to correct those tags?
This could be done only manually, by grabbing the content of this LINK (via PHP) and rename those ones?
I do not have the file from my clients, just the LINK of XML.
thanks in advance!
ok! I believe I have found some solutions to my problem.. I wrote them here in case someone has the same issues:
Solutions:
i) Read all the children of XML file, no matter how they are written (case-sensitive) and add them to Database. After that, there is a dashboard/PHP file with SQL queries that MATCH those children elements tags of XML with the one that you want.
In this case, you may want to create a file called whatever you like, for example test.xml and CREATE the one that you want, with the correct XML tag elements. In this case, you could UPDATE this, every some hour (according to your needs) via a cronjob..
ii) Create manually the PHP file with the parsing inside, for every XML that you get. Just make sure to keep the XML link in your DB
iii) Ask the client to give you the correct XML. XML is case-sensitive for a reason.
In case you choose the first solution you need to make changes to php.ini file too, cause the XML files may be too large and the max_execution_time is probably too low to run all these PHP - MySQL scripts.
if someone need more explain or have any better advice, please share!

PHP use XML file as a data repository

I have a simple PHP application, which uses MySQL DB, but I think that maybe the using of DB is needlessly for such easy operations.
Anyway, I hove some problems with the XML operations.
Let's say I want to have XML structure like this:
<root>
<experiment>
<name>test</name>
<accessCount>5</accessCount>
<downloadEntry>
<date>2015-11-27</date>
<comment>comment</comment>
</downloadEntry>
<downloadEntry>
<date>2015-11-28</date>
<comment>comment</comment>
</downloadEntry>
</experiment>
</root>
Now I would like to know, how to do these operations:
Count download entries (count of downloadEntry nodes) of experiment with name "test". Via XPATH?
Get download entries of experimetn with name test - but I would like to have pagination on this. So get download entries somehow like LIMIT 0,5.
The biggest problem is that, when there are no experiments - so the XML is , the loading of XML with simplexml_load_file fails. I can't open it. Yes, I can add the condition - if the XML is empty, donť open it. But I need to write to it and can't write if it isnť open.
Is there a solution for that?
Thanks everyone

PHP XML DOM looping through complex xml records with varying levels

I'm completely unfamiliar with PHP XML DOM, Any help appreciated.
I've been using the site and going through previous answers on this, and anywhere else I could find online, but I can't seem to find a good PHP XML DOM tutorial, my ultimate aim is to take a complex XML and use PHP to upload this into a mysql db. Can anyone recommend a good PHP XML DOM tutorial to do this?
I want to take all the child elements from an XML and put them into an associative array. So in the below 'entry' XML record it would get as far as 'ent_seq' realise there are no sub-nodes, and assign that to a value $array = ($ent_seq[$counter]=>1636730), then the loop would realise the next node is and realise this has subnodes, and choose to assign these subnodes as $array = ($ent_seq[$counter]=>1636730, $keb[$counter]=>通い, $ke_pri[$counter]=>news1); and continue on like that, breaking out the subnodes as appropriate.
<entry>
<ent_seq>1636730</ent_seq>
<k_ele>
<keb>通い</keb>
<ke_pri>news1</ke_pri>
<ke_pri>nf13</ke_pri>
</k_ele>
<r_ele>
<reb>かよい</reb>
<re_pri>news1</re_pri>
<re_pri>nf13</re_pri>
</r_ele>
<sense>
<pos>&n;</pos>
<gloss>coming and going</gloss>
<gloss>commuting</gloss>
</sense>
</entry>
I've not included any of the code I've come up with as its not helpful and it just selects the nodes at the ent_seq level, so for each record its only storing a few pieces of data, and all of the subnodes within the same record.
If anyone could point me in the right direction It would be appreciated.

PHP in URL to locate specific node in XML db

i'm totally new to XML, but am trying it out on a site of mine to iterate through a very easy db. what i can't figure out is how to post a link outside of the site to a specific node in the xml database. the site in question is (http://thenewsomething.com)
please forgive any obvious ignorance as this is my first attempt with xml. what i'd like to do is post a url elsewhere that will link to thenewsomething.com with one of the specific nodes. for instance, if i give each node an id, can i do something like
http://thenewsomething.com?id=5
? thanks for your help in advance!
Sure. In PHP, you can look at $_GET['id'] to read out the ID passed to your script by the URL. (Make sure you escape the input by using something like addslashes). If your script is called index.php and someone browses to index.php?id=5, you can do this:
<?php
$id = addslashes($_GET['id']);
#use code tailored to your specific database to fetch the node with this ID
#display it
?>
You can look into something like SimpleXML and XPath to search within your XML database. For a quick introduction to XPath, consider:
http://www.w3schools.com/xpath/xpath_syntax.asp
If you post details of your XML schema, we may be able to help more.

read an xml file into database

I want to store the contents of an xml file in the database. Is there an easy way to do it?
Can i write some script that can do the task for me?
The schema of the XML file looks like this:
<schedule start="20100727120000 +0530" stop="20100727160000 +0530" ch_id="0210.CHNAME.in">
<title>Title_info</title>
<date>20100727</date>
<category>cat_02</category>
</schedule>
One thing to note is:
How do I read the start time? I need the time +0530 added to the time?
Thank you so much.
You'll probably want to create a table called schedules that matches your data, then read the contents of the XML file with an XML parser of your choice. SimpleXML might be the right tool for this job.
As for the dates, I recommend you try using the function date_parse_from_format().
look up simple_xml on the php page - off hand I'm not too hot on it, but basically you will end up with a loop which will add your data to an object eg:
$xml
and you will be able to call tags as such $xml->schedule->title $xml->schedule->date and $xml->schedule->category and you will be able to call attributes as such $xml->schedule[start] but you might wanna check that.
I had to do this recently for a client, and this was the best way I could find. The attributes may be tricky - I can't quite remember but you might have to look into namespaces and such... anyway, find simple_xml and you're on the right tracks.

Categories