XML file content not updating using PHP - php

Each time I run the code, file updates and I can see the file last edited date and time are updated but the content in the XML file is not updated.
I just tried to update the following XML Code
<?xml version="1.0" encoding="utf-8"?>
<topcont>
<sitenondualtraining>
<title>The Heart of Awakening</title>
<descripition>nondual</descripition>
<link>www.test.com/post/latestpost</link>
</sitenondualtraining>
</topcont>
using PHP code
$topcont = new DOMDocument();
$topcont->load("http://fenner.tk/topcont.xml");
$topcont->topcont->sitenondualtraining->title = 'test';
$topcont->sitenondualtraining->descripition = $_POST['nd2'];
$topcont->sitenondualtraining->link = $_POST['nd3'];
$topcont->Save("topcont.xml");
I also tried
$topcont = new SimpleXmlElement('http://fenner.tk/topcont.xml',null, true);
$topcont->sitenondualtraining->title = $_POST['nd1'];
$topcont->sitenondualtraining->descripition = $_POST['nd2'];
$topcont->sitenondualtraining->link = $_POST['nd3'];
$topcont->asXml('topcont.xml');
But none of these are working. Can anyone point where the issue is? Thanks.
File permission are set to 777 but still not working
NO ERRORS BUT WARNINGS ARE
Warning: Creating default object from empty value in /home/fenner/public_html/topads.php on line 20
Warning: Creating default object from empty value in /home/fenner/public_html/topads.php on line 21 /home/fenner/public_html/

Using DomDocument, you were almost there. You can do it like this:
$topcont = new DOMDocument();
$topcont->load("topcont.xml");
$topcont->getElementsByTagName("title")->item(0)->nodeValue = $_POST['nd2'];
$topcont->getElementsByTagName("description")->item(0)->nodeValue = $_POST['nd2'];
$topcont->getElementsByTagName("link")->item(0)->nodeValue = $_POST['nd3'];
$topcont->save("topcont.xml");
Just remember to sanitize your inputs before storing your data ;)
Also worth looking into is creating cdata sections and using replaceData, depending on what you intend to store in each node.
EDIT
In response to your comment below, you might want to change your xml structure a little if you are going to be handling multiple child nodes. This way it is easier to loop through and update the node you are interested in. You will see below that I moved 'sitenondualtraining' and 'siteradiantmind' to be id's of an 'item" node, though you could easily change this to something like <site id="nodualtraining> if that's more like what you were looking for.
<?xml version="1.0" encoding="utf-8"?>
<topcont>
<item id="sitenondualtraining">
<title>test</title>
<description>hello test</description>
<link>hello</link>
</item>
<item id="siteradiantmind">
<title>The Heart of Awakening</title>
<description>radiantmind</description>
<link>www.radiantmind.com/post/latestpost</link>
</item>
</topcont>
Your PHP code would then be something like this, again this is quite basic and could be tidied up, but is a good start:
$items = $topcont->getElementsByTagName("item");
// loop through each item
foreach ($items as $item) {
$id = $item->getAttribute('id');
// check the item id to make sure we edit the correct one
if ($id == "sitenondualtraining") {
$item->getElementsByTagName("title")->item(0)->nodeValue = $_POST['nd1'];
$item->getElementsByTagName("link")->item(0)->nodeValue = $_POST['nd2'];
$item->getElementsByTagName("description")->item(0)->nodeValue = $_POST['nd3];
}
}
If you were feeling a little adventurous, you could have a look at xpath and xpath query, you can find some sample code in most php docs to get you started and the comments from other users can be helpful as well.
For reference: getAttribute, getElementsByTagName.

Related

Php retrieve XML elements and edit file on server

I've searched and tried a number of examples and answers I've found here, but I think my requirements are quite specific and I haven't been able to find an answer that's worked for me so far... Bodging a number of answers together hasn't worked either!
Aim - Update the hosplist_divert value of the same element that contains a specific name tag.
The XML file is hosted in a sperate folder to the php page, in those case at /data/hospitals2.xml
My XML is like so:
<Document>
<Placemark>
<name>UHSM Wythenshawe</name>
<hosplist_divert>1</hosplist_divert>
</Placemark>
</Document>
There are approx 50 Placemark entries in the file.
So far I am only able to return all the hospital names from the file with,
// Create the SXE object
// You can read from file using the simplexml_load_file function
$url = "http://www.patientpathfinder.co.uk/user/nwasdos/data/hospitals2.xml";
$sxe = new SimpleXMLElement($url, NULL, TRUE);
$sxe->registerXPathNamespace('hospital','http://earth.google.com/kml/2.2');
// Fetch the right HOSPITAL using XPATH
// hospital name stored in Document/Placemark/name
// dovert status stored in Document/Placemark/hosplist_divert
//trying to find above values for UHSM Wythenshawe
$result=$sxe->xpath('//hospital:name[.="UHSM Wythenshawe"]/parent::*');
foreach ($result as $hospital)
{
echo $hospital . "<br>";
}
// Update the values you want
//$target_hosp[0]->hosplist_divert = 'ON DIVERT';
// Store the updated values in the $xml variable
//$xml = $sxe->asXML();
// Print the updated XML
//echo $xml;
}
It took me about half a day to realise that I needed to define the namespace, but haven't really had time to understand why the namespace is required and would be happy to remove it from the XML file in. Favour of a working solution.
Thanks to all that contribute,
Nick

unset() Not Working For XML

Currently I am using unset() for removing a parent node in simpleXML, and writing it back to the XML file
I tried this code and it was working a while ago, after cleaning my code I can't find why it doesn't work all of the sudden,
The debugging approaches I took: the file can be accessed, I can enter the loop and if statement, the file gets saved (notepad++ asks me to reload), but the <systemInfo></systemInfo> does not get deleted
Here is my sample Code:
$userorig = $_POST['user'];
$userinfos = simplexml_load_file('userInfo.xml'); // Opens the user XML file
foreach ($userinfos->userinfo->account as $account)
{
// Checks if the user in this iteration of the loop is the same as $userorig (the user i want to find)
if($account->user == $userorig)
{
echo "hello";
$rootSystem = $account->systemInfo;
unset($rootSystem);
}
}
$userinfos->saveXML('userInfo.xml');
My XML File:
<userinfos>
<userinfo>
<account>
<user>TIGERBOY-PC</user>
<toDump>2014-03-15 03:20:44</toDump>
<toDumpDone>0</toDumpDone>
<initialCheck>0</initialCheck>
<lastChecked>2014-03-16 07:12:17</lastChecked>
<alert>1</alert>
<systemInfo>
... (many nodes and sub nodes here) ...
</systemInfo>
</account>
</userinfo>
</userinfos>
Rather than iterating over the whole xml, use xpath to select the node:
$userorig = $_POST['user'];
$userinfos = simplexml_load_file('userInfo.xml'); // Opens the user XML file
$deletethisuser = $userinfos->xpath("/userinfos/userinfo/account[user = '$userorig']/systemInfo")[0];
unset($deletethisuser[0]);
Comments:
the [0] in the xpath... line requires PHP >= 5.4, in case you are running on a lower version, either update or go:
$deletethisuser = $userinfos->xpath("/userinfos/userinfo/account[user = '$userorig']/systemInfo");
unset($deletethisuser[0][0]);
Advised reading: hakre's answer in this thread: Remove a child with a specific attribute, in SimpleXML for PHP
It worked again, sorry, I did not know why it worked, I keep running it on multiple instances, and now it works, the program has weird behavior, but tried it for around 15 tries, it did its job

Getting node Value of last Child

This question may seem very stupid, but I am not able to find much help on how to find the node value of the last child using PHP, even though it's a piece of cake with JS.
This is what my XML currently looks like:
<?xml version="1.0"?>
<files>
<file>.DS_Store</file>
<file>ID2PDF_log_1.xml</file>
<file>ID2PDF_log_12.xml</file>
<file>ID2PDF_log_15.xml</file>
</files>
Here's the php code:
$filename = 'files.xml'; //my xml file name
$dom = new DomDocument();
$dom->load($filename);
$elements = $dom->getElementsByTagName('file');
echo $elements->lastChild(); // This is obviously not working
/*I get an error that I am trying to access an undefined method in DOMNodeList. Now, I know
that lastChild is a property of DOMNode. But I can't figure out how I can change my code to
get this to work.*/
I am trying to echo out
ID2PDF_log_15.xml
Can anyone show me how to get this done?
P.S.: I don't want to change the xml file structure because I am creating it through a script and I am a lazy programmer. But, I did do my research to get this. Didn't help.
I did try getting the number of elements in the node 'file' and then using item(#), but that didn't seem to work either.
Thanks
SOLUTION
$filename = 'files.xml';
$dom = new DomDocument();
$dom->load($filename);
$elements = $dom->getElementsByTagName('file')->length;
echo 'Total elements in the xml file:'.$elements."\n";
$file = file_get_contents('files.xml');
$xml = simplexml_load_string($file);
$result = $xml->xpath('file');
echo "Last element".$result[$elements-1]."\n";
I'll make this neater a little later. But, just thought that I should share the answer anyway any new users in the future.
This should work:
$elements->xpath('root/child[last()]');
Read up about xpath
Alternatively I would suggest counting the number of elements, and then targeting the last element using that count:
$file_count = $elements->getElementsByTagName('file')->length;
$elements[$file_count];
i did it this way:
$elements = $dom->getElementsByTagName('file')->item(0);
echo $elements->lastChild->nodeValue;

Read XML file and write to a php array/file

I need to update the country list of my website and I want to automate the process. Country list can be found here
http://www.iso.org/iso/country_codes...code_lists.htm // Edit : Can't find the good link...
I tried it this way –
http://www.w3schools.com/php/php_xml_parser_expat.asp (PHP XML Expat Parser)
However, this didn't seem to work well as I was confused where to actually 'get' the data and print it to my own array for later use.
Now I want to try it using XML DOM.
Just want to check with everyone, if I had a simple XML file to read, that contained a country code and country name as follows:
<Entry>
<Country_name>AFGHANISTAN</Country_name>
<Code_element>AF</Code_element>
</Entry>
I want to read this file (DOM method), and then feed the data into a separate file/array of mine that will be accessed by my website. What PHP xml functions would YOU use/recommend to do this simple task?
Any help in this regards is appreciated.
Use SimpleXML
how about
$dom = new DOMDOcument();
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);
$res = $xpath->query("/CODE");
$allres = array();
foreach($res as $node){
$result = array();
$result['country'] = ($node->getElementsByTagName("Country_name")->item(0)->nodeValue);
$result['code'] = ($node->getElementsByTagName("Code_element")->item(0)->nodeValue);
$allres[] = $res
}
in the end $allres array would contain all your country codes and names

How do I serialize DOM into XML using PHP?

I am attempting to send html data in a question form from my php web application to mechanical turk so a user can see the entire html document from an email to work with. I have had difficulty thus far. In the thread linked below, I attempted to parse the html data using html5-lib.php, but I think I'm still missing a step in order to complete this.
Here's the current error I'm receiving:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoeshere.php on line 35
Here's the current code I'm working with...
$thequestion = 'click here';
$thequestion = HTML5_Parser::parseFragment($thequestion);
var_dump($thequestion);
echo $thequestion;
//htmlspecialchars($thequestion);
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text>'.$thequestion.'</Text> //<--- Line35
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
I'm not 100% sure if the parser is what I need to do in order to have this sent correctly - All I want to do is send html through this xml type document - I'm surprised it's been this difficult thus far.
This is somewhat of a continuation of another thread -
What PHP code will help me parse html data in an xml form?
Take a look at DOMDocument for working with DOM/xml in PHP. If you want to embed HTML in your XML, use CDATA sections like this:
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text><![CDATA['.$thequestion.']]></Text>
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
Not sure exactly what you're after. This is how i would create the XML needed to be transferred.
Please let me know if i misunderstood the question
It also seems that you are missing the QuestionIdentifier node which is mandatory according to the .xsd file.
<?
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$QuestionForm = $dom->createElement('QuestionForm');
$QuestionForm->setAttribute('xmlns','http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd');
// could loop this part for all the questions of the XML
$thequestion = 'click here';
//Not sure what this is supposed to be, but its required. Check the specs of the app for it.
$questionID = "";
$Question = $dom->createElement('Question');
$QuestionIdentifier = $dom->createElement('QuestionIdentifier',$questionID);
$QuestionContent = $dom->createElement('QuestionContent');
$QuestionContent->appendChild($dom->createElement('Text',$thequestion));
$AnswerSpecification = $dom->createElement('AnswerSpecification');
$AnswerSpecification->appendChild($dom->createElement('FreeTextAnswer'));
$Question->appendChild($QuestionIdentifier);
$Question->appendChild($QuestionContent);
$Question->appendChild($AnswerSpecification);
$QuestionForm->appendChild($Question);
// End loop
$dom->appendChild($QuestionForm);
$xmlString = $dom->saveXML();
print($xmlString);
?>

Categories