PHP To Edit Existing XML Entry - php

Ok, please bear with me as I explain this app. The first view shows a tableview, where each row is a 'request' that someone has made, also using the app. The requests are actually a parsed xml file that looks something like this:
<item>
<title>Request</title>
<name>Name</name>
<Responses>0</Responses>
</item>
Using the script listed underneath this paragraph, I am able to run some code in iOS that will take text from text-boxes in the app, and append the XML with a new item set.
<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$request = $_POST['request'];
$anon = $_POST['anon'];
$pubDate = $_POST['pubDate'];
$loc = $_POST['loc'];
//This line will load the XML file
$xml = simplexml_load_file("URL.xml") or die("Not loaded!\n");
//print_r($xml);
//This line gets the channel element (from an array returned by the xpath method)
$channel = $xml->xpath('//channel');
$channel = $channel[0];
//print_r($channel);
$person = $channel->addChild("item");
$person->addChild("first_name", $firstName);
$person->addChild("last_name", $lastName);
$person->addChild("title", $request);
$person->addChild("date", $pubDate);
$person->addChild("anonymous", $anon);
$person->addChild("responses", "0");
$person->addChild("location", $loc);
//This next line will overwrite the original XML file with new data added
$xml->asXML("Test.xml");
?>
The actual XML has much more listed than what I put at the beginning, I just edited it down to save space, so that's why it and the PHP don't quite match up.
All of this works great, the app parses the XML to show what has been requested, and a user can make a new request that will then show in the app.
My problem is that when someone responds to an existing request, I would like the <Responses> to increase by 1. Is there something I can do with a different script that would find that entry, and +1 to whatever number currently exists in that item's Response tag?
I have seen this script, but it appears to go through and search for all of a certain tag, and change all tags of that name in the XML. I am looking to just find one specific one and change it. Suggestions?
$str = <<<XML
<ScrapBook>
<Event>
<Name> Name of Event </Name>
<Blah>glop glop</Blah>
</Event>
</ScrapBook>
XML;
$xmlDoc = new DOMDocument();
$xmlDoc->loadXml($str);
$events = $xmlDoc->getElementsByTagName("Event");
foreach($events as $event){
$eventNames = $event->getElementsByTagName("Name");
$eventN = $eventNames->item(0)->nodeValue;
if(' Name of Event ' == $eventN){
$eventNames->item(0)->nodeValue = 'New name';
}
}
var_dump($xmlDoc->saveXML());

You can go with simplexml to do that; first, select the <item> to be changed, then store the new <Responses>:
$xml = simplexml_load_string($x); // assume XML in $x
// select <Responses> with xpath by title and name
$responses = $xml->xpath("/items/item[title = 'Foo' and name = 'Bar']/Responses");
// store number + 1
$responses[0][0] = $responses[0] + 1;
see it working: https://eval.in/127882

Related

Reading and writing Associative array to XML file

I am trying to add an array $item to an XML file in order to then be able to read all of the items in a later time.
I have the following PHP to perform this action:
<?php
$item = array();
$item['rating'] = $_GET['rating'];
$item['comment'] = $_GET['comment'];
$item['item_id'] = $_GET['item_id'];
$item['status'] = "pending";
//Defining $xml
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($item, array($xml, 'addChild'));
$xml = $xml->asXML();
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
//Save XML as a file
$dom->save('reviews.xml');
However, when run what I get this in my XML file:
< ?xml version="1.0"?>
Basically my array is no where to be seen.
A var_dump of $item gives
array(4) { ["rating"]=> string(1) "8" ["comment"]=> string(17) "I Really Like it!" ["item_id"]=> string(1) "9" ["status"]=> string(7) "pending" }
How could I modify my code in order to have it save an array (and if there are many keep them all) in the file reviews.xml?
Also How could I make it so that later on I would be able to access the data; for instance changing the status from pending to approved?
EDIT:
Using the following code I have been able to save my item to the file:
$item = array();
$item[$_GET['rating']] = 'rating';
$item[$_GET['comment']] = 'comment';
$item[$_GET['item_id']] = 'item_id';
$item['pending'] = 'status';
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($item, array($xml, 'addChild'));
$xml->asXML('reviews.xml');
However I am still unable to append new data to the root rather than overwriting the current saved data.
As I was saying in my comment... The code you provided errors with WARNING DOMDocument::loadXML(): Empty string supplied as input. You never assigned anything to $xml'...
Proper error reporting/logging would help spot these mistakes.
<?php
$item = array();
$item['rating'] = 'a';
$item['comment'] = 'b';
$item['item_id'] = 'c';
$item['status'] = "pending";
//Defining $xml
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($item, array($xml, 'addChild'));
//THIS IS THE LINE YOU WERE MISSING
$xml = $xml->asXML();
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
//Save XML as a file
$dom->save('reviews.xml');
If you echoed it out...
var_dump($dom->saveHTML());
> string(80)
> "<root><a>rating</a><b>comment</b><c>item_id</c><pending>status</pending></root>"
Please avoid updating your existing question with additional questions.
A database would make the task easier. Using a flat file works fine though, XML, or some other format. You will need to be able to retrieve a record by item_id, at which point you modify it, then replace it. That is the gist of it.
So here's an overhaul of your code, with some changes to both your approach and the scheme of your XML, based on your various comments and updates.
So first, instead of creating XML that looks like this:
<root>
<rating>a</rating>
<comment>b</comment>
<item_id>c</item_id>
<status>pending</status>
</root>
You're going to store the XML like this:
<root>
<item id="c">
<rating>a</rating>
<comment>b</comment>
<status>pending</status>
</item>
</root>
This is based on a few of your comments:
You are wanting to add to the XML file rather than overwrite the existing file content. That suggests that you want to store multiple items. This would also explain why you have a property item_id. So rather than having a mess of XML like :
<root>
<rating>a</rating>
<comment>b</comment>
<item_id>c</item_id>
<status>pending</status>
<rating>d</rating>
<comment>e</comment>
<item_id>f</item_id>
<status>pending</status>
<rating>g</rating>
<comment>h</comment>
<item_id>i</item_id>
<status>pending</status>
</root>
where it is impossible to know which item is which, you store each set of item properties on an <item> element. Since you are going to want to easily grab an item based on its item_id in order to update that item, making item_id an attribute of the <item> makes more sense than making it a child of the <item>.
You want to be able to update the status. This is where having the item_id stored on the item comes in handy. If someone submits a request with an existing item_id, you can update that item, including its status element. Or you could do it whenever you need to from some other process, etc.
Here's the code I drummed up for this. Note that it currently isn't set up to look for an existing element with that item id, but that should be possible using existing SimpleXML functions/methods.
$item = array();
$item_id = "c";
$item['rating'] = 'a';
$item['comment'] = 'b';
$item['status'] = "pending";
$xml = simplexml_load_file('ratings.xml');
//if ratings.xml not found or not valid xml, create clean XML with <root/>
if($xml === false) {
$xml = new SimpleXMLElement('<root/>');
}
$xml_item = $xml->addChild("item");
$xml_item->addAttribute("id", $item_id);
foreach($item as $name => $value) {
$xml_item->addChild($name, $value);
}
$xml->asXML('ratings.xml');
Notice that one of the major changes I made to your existing code is changing from using array_walk_recursive to a simple foreach. array_walk_recursive for this purpose is a short cut that causes more issues than it solves. For instance, you had to swap your key and value on the $item array, which is confusing. It also isn't necessary for what you currently are doing, since you don't have a multi-dimensional array. And even if you did, array_walk_recursive isn't the right choice to handle looping over the array recursively because it would add each array member to the root of the XML, not add sub-arrays as children of their parent entry as they show up in the actual array. Point being, it's confusing, it doesn't add any value, and using a foreach is a lot more clear on what you are actually doing.
I've also changed
$item['item_id'] = 'c';
to
$item_id = 'c';
and then added it to the item element as an attribute like:
$xml_item->addAttribute("id", $item_id);
This is consistent with the new schema I outlined earlier.
Finally, instead of passing the XML to DOMDocument, I'm just using
$xml->asXML('ratings.xml');
SimpleXML already removes any extra whitespace, so there is no need to use DOMDocument to achieve this.
Based on some of the counterintuitive parts of your original code, it looks like you may have done a decent amount of copy and pasting to get it going. Which is where most of us start, but it's a good idea to be upfront about things like "I don't understand quite what this code is doing, I just grabbed it from a script that did some of what I need." It will save us all a lot of time and grief if we're not assuming you are using the code you have because you need to or it was a conscious decision, etc, and that we have to work within the constraints of that code.
I hope this gets you off to a good start.
Update
I was messing around with it, and came up with the following for updating existing <item> if an item with id set to $item_id already exists. It's a bit clunky, but it tested and it works.
This assumes the $item_id and $item array get set as normal, as well as retrieving the exiting XML, as covered above. I'm providing the lines just before the changes for reference:
$xml = simplexml_load_file('ratings.xml');
//if ratings.xml not found or not valid xml, create clean XML with <root/>
if($xml === false) {
$xml = new SimpleXMLElement('<root/>');
}
//query with xpath for existing item with $item_id
$item_with_id = $xml->xpath("/root/item[#id='{$item_id}']");
// if the xpath returns a result, update that item with new values.
if(count($item_with_id) > 0) {
$xml_item = $item_with_id[0];
foreach($item as $name => $value) {
$xml_item->$name = $value;
}
} else {
// if the xpath returns no results, create new item element.
$xml_item = $xml->addChild("item");
$xml_item->addAttribute("id", $item_id);
foreach($item as $name => $value) {
$xml_item->addChild($name, $value);
}
}

How to add the attribute value for elements(with same name) in xml using SimpleXML PHP programatically

Here i am creating nodes as a child to the AdSources node. I can able to create several nodes with same name "Ad". But i am not able to set the attribute("id") for each element("Ad"). If i try to set ID value for the second element, it says that this element already has the same attribute. i.e each time when i try to set id for it goes on to the first element alone.
$xmlFile = 'mediationrequest2.xml';
$xml = new SimpleXMLExtended('<Mediation version="1.0"/>');
$adsources = $xml->addChild('AdSources');
$xml->AdSources[0]->addChild('Ad');
$xml->AdSources[0]->Ad->addAttribute('id', '1001');
$adsources->Ad[0]->addChild('Value', '5');
$adsources->Ad[0]->addChild('VASTAdTagURI');
$xml->AdSources[0]->Ad[0]->VASTAdTagURI->addCData('http://rtr.innovid.com/r1.515f10ae711057.99306980;cb=$rand');
$xml->AdSources[1]->addChild('Ad');
$xml->AdSources[0]->Ad->addAttribute('id', '1002');
$adsources->Ad[0]->addChild('Value', '5');
$adsources->Ad[0]->addChild('VASTAdTagURI');
$xml->AdSources[0]->Ad[0]->VASTAdTagURI->addCData('http://rtr.innovid.com/r1.515f10ae711057.99306980;cb=$rand');
SimpleXMLElement::addChild() returns the new element object. Use the variable.
$xml = new SimpleXMLElement('<Mediation version="1.0"/>');
$adsources = $xml->addChild('AdSources');
$Ad = $adsources->addChild('Ad');
$Ad->addAttribute('id', '1001');
$Ad = $adsources->addChild('Ad');
$Ad->addAttribute('id', '1002');
echo $xml->asXml();
Output:
<?xml version="1.0"?>
<Mediation version="1.0">
<AdSources><Ad id="1001"/><Ad id="1002"/></AdSources>
</Mediation>

PHP - Retrieving Only Certain XML Nodes and Saving to a file

I am using PHP curl to retrive an xml file from a remote url and save it to a local file on my server. The structure is the following:
<Store Country="Ireland">
<EventsPoints>
<Event ID="1800" >
<ArtistIDs>
<ArtistID ID="109" Type="Primary" />
</ArtistIDs>
<CategoryID>1</CategoryID>
<Country>IRL</Country>
<PerformanceName>Music and Arts</PerformanceName>
<VenueID ID="197" />
</Event>
<Venues>
<Venue ID="197">
<City>Dublin</City>
<Country>IRL</Country>
<VenueName>ABC</VenueName>
<VenueNumber>22</VenueNumber>
</Venue>
</Venues>
The above xml blocks are stored in the same XML file. There are several Event blocks and several Venue blocks.
The problem i'm having is using PHP to access this large XML file and iterate through the Venue blocks retrieving only a Venue block with a certain ID specified via a parameter.
I then want to iterate through the event blocks - only retrieving the events matching this specified venue ID. I then want to save this to a file on the server.
I want to do this for each venue.
How do I go about doing the above?
EDIT:
For each Venue and events related to that venue, I just want to literally copy them to their own file - basically splitting down the larger file into individual files
$docSource = new DOMDocument();
$docSource->loadXML($xml);
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('/var/www/html/xml/testfile.xml'));
$xpath = new DOMXPath($docSource);
$id = "197";
$result = $xpath->query('//Event/VenueID[#ID=$id]')->item(0); //Get directly the node you want
$result = $docDest->importNode($result, true); //Copy the node to the other document
$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document
echo $docDest->saveXML();
You are not showing the desired output format, so I will assume this generates what you want. If not, feel free to modify the code so it meets the desired output format. This along with the comments above should have all you need to get this working on your own.
// load Source document
$srcDom = new DOMDocument;
$srcDom->load('/var/www/html/xml/testfile.xml');
$xPath = new DOMXPath($srcDom);
// iterate over all the venues in the source document
foreach ($srcDom->getElementsByTagName('Venue') as $venue) {
// create a new destination document for the current venue
$dstDom = new DOMDocument('1.0', 'utf-8');
// add an EventsPoint element as the root node
$dstDom->appendChild($dstDom->createElement('EventsPoint'));
// import the Venue element tree to the new destination document
$dstDom->documentElement->appendChild($dstDom->importNode($venue, true));
// fetch all the events for the current venue from the source document
$allEventsForVenue = $xPath->query(
sprintf(
'/Store/EventsPoints/Event[VenueID/#ID=%d]',
$venue->getAttribute('ID')
)
);
// iterate all the events found in Xpath query
foreach ($allEventsForVenue as $event) {
// add event element tree to current destination document
$dstDom->documentElement->appendChild($dstDom->importNode($event, true));
}
// make output prettier
$dstDom->formatOutput = true;
// save XML to file named after venue ID
$dstDom->save(sprintf('/path/to/%d.xml', $venue->getAttribute('ID')));
}
This will create an XML file like this
<?xml version="1.0" encoding="utf-8"?>
<EventsPoint>
<Venue ID="197">
<City>Dublin</City>
<Country>IRL</Country>
<VenueName>ABC</VenueName>
<VenueNumber>22</VenueNumber>
</Venue>
<Event ID="1800">
<ArtistIDs>
<ArtistID ID="109" Type="Primary"/>
</ArtistIDs>
<CategoryID>1</CategoryID>
<Country>IRL</Country>
<PerformanceName>Music and Arts</PerformanceName>
<VenueID ID="197"/>
</Event>
</EventsPoint>
in the file 197.xml

Can't save to XML file using domXML

I have been trying to make a method for adding new elements in an XML file, but for some reason it does not modify the element, even that when i Use saveXML prints the right ones.
function fnDOMEditElementCond($product_id, $name, $weight, $category, $location) {
if (!isset($product_id) || is_numeric($product_id)) {
return false;
}
$obj = new helperClass();
$xmlDoc = $obj->fetchFromXMLDocument('storage.xml');
$xpath = new DOMXPath($xmlDoc);
$result = $xpath->query(sprintf('/storagehouse/item[#id="%s"]', $product_id));
if (!$result || $result->length !== 1) {
throw new Exception(sprintf('Item with id "%s" does not exists or is not unique.', $product_id));
}
$item = $result->item(0);
//Change the name element
$xName = $xpath->query("./name", $item)->item(0);
$xName->removeChild($xName);
// //Change the name element
// $xWeight = $xpath->query("./weight", $item)->item(0);
// $xWeight->nodeValue = $weight;
//
// //Change the name element
// $xLocation = $xpath->query("./location", $item)->item(0);
// $xLocation->nodeValue = $category;
//
// //Change the name element
// $xCategory = $xpath->query("./category", $item)->item(0);
// $xCategory->nodeValue = $location;
echo $xmlDoc->saveXML($item);
}
}
This code takes a ID, and depending on the id it modifies the information, on four elements.
Here is the xml fil
storagehouse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<item id="c7278e33ef0f4aff88da10dfeeaaae7a">
<name>HDMI Cable 3m</name>
<weight>0.5</weight>
<category>Cables</category>
<location>B3</location>
</item>
<item id="df799fb47bc1e13f3e1c8b04ebd16a96">
<name>Dell U2410</name>
<weight>2.5</weight>
<category>Monitors</category>
<location>C2</location>
</item>
<item id="53abbd89766ea8759b5ebe5bacd43f58">
<name>HP Probook 1311</name>
<weight>2.1</weight>
<category>Notebooks</category>
<location>A1</location>
</item>
<storagehouse/>
Do you have any idea on why that it might not working?
I believe it is because you are looking for save() not savexml(). And you will need to give save() the filename.
Edit: Also, if I'm remembering correctly, you will need to reload the DOM after each save, otherwise you will only effect the unsaved DOM elements. But it's been a while since I've had to do it. I'll take a look through my old code to double check.
Edit Again: I've been looking through my code, and it does appear to be necessary. You can do all the modifications you need at once, but if you need to view the results in the interim, you either have to produce it with saveXML() or save() it and read it into a fresh XML object. The original object will only reflect the original content. I didn't properly note it in my comments like I should have, but my code is done in such a way that it makes me think I am right. Hopefully this helps.

PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error'

I am trying to learn XML and I know this is a problem with not properly importing the Nodes. But I can't quite figure it out. I've been looking around and most people don't have multiple child elements like I do with the departments.
Here is my XML structure:
<SOT>
<DEPARTMENT name="Aviation Technology" id="AT">
<EMPLOYEE type="Faculty">
<LOGIN>jdoe1</LOGIN>
<NAME>John Doe</NAME>
</EMPLOYEE>
<EMPLOYEE type="Faculty">
<LOGIN>jdoe2</LOGIN>
<NAME>Jane Doe</NAME>
</EMPLOYEE>
<EMPLOYEE type="Faculty">
<LOGIN>jdoe3</LOGIN>
<NAME>Joe Doe</NAME>
</EMPLOYEE>
</DEPARTMENT>
<DEPARTMENT name="Building and Construction Management" id="BCM">
</DEPARTMENT>
<DEPARTMENT name="Computer Graphics Technology" id="CGT">
</DEPARTMENT>
</SOT>
I understand that SOT is my root element and that departments are "children" of SOT and each department has multiple employee "children". The problem I run into is when I am trying to add a new employee to a certain department. When I try $departmentArray->item($i)->appendChild($employee); I get the Wrong Document Error.
I'm using this PHP code to try and append the child to the departmentNode
<?php
//grab form data
$username = $_POST['username'];
$employeeName = $_POST['employeeName'];
$department = $_POST['department'];
//create new DOMDocument to hold current XML data
$doc = new DOMDocument();
$doc->load("test.xml");
$xpath = new DOMXpath($doc);
//create our new DOMDocument for combining the XML data
$newDoc = new DOMDocument();
$newDoc->preserveWhiteSpace = false;
//create School of Tech Node and append to new doc
$sotElement = $newDoc->createElement("SOT");
$newDoc->appendChild($sotElement);
$root = $newDoc->documentElement;
//grab the department Nodes
$departmentArray = $doc->getElementsByTagName("DEPARTMENT");
//create a new employee and set attribute to faculty
$employee = $newDoc->createElement("EMPLOYEE");
$employee->setAttribute("type", "Faculty");
//counters (might use them later for ->item(counter) function
$indexCounter = 0;
$i = 0;
foreach($departmentArray as $departmentNode){
if(strcmp($departmentNode->getAttribute('name'),$department) == 0){//check if departments match
//create login element
$loginNode = $newDoc->createElement("LOGIN");
$loginNode->appendChild($newDoc->createTextNode($username));
$employee->appendChild($loginNode);
//create name node
$nameNode = $newDoc->createElement("NAME");
$nameNode->appendChild($newDoc->createTextNode($employeeName));
$employee->appendChild($nameNode);
//append employee onto department node
//$departmentArray->item($i) = $doc->importNode($departmentArray->item($i), true);
$departmentArray->item($i)->appendChild($employee);
//set index of department array (possibly used for appending later)
$indexCounter = $i;
}
$i++;
}
#######################################
/*Write out data to XML file */
#######################################
//$departmentArray = $doc->getElementsByTagName("DEPARTMENT");
foreach($departmentArray as $departmentNode){
$tempNode = $newDoc->importNode($departmentNode, true);
/*if(strcmp($departmentNode->getAttribute('name'),$department) == 0){
$sotElement->appendChild($employee);
}*/
$sotElement->appendChild($tempNode);
}
$newDoc->formatOutput = true;
$newDoc->save("test2.xml");
?>
Any help explaining how to properly import all the department nodes to be able to append onto them would be greatly appreciated. I've tried using arrays.
You need to import any node to append it to another document:
$departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
I'm pretty sure that this is happening because you are trying to append an element from a different document into your output document.
I found this code in a comment on php's site for DOMNode::cloneNode which might be what you want.
<?php
$dom1->documentElement->appendChild(
$dom1->importNode( $dom2->documentElement, true )
);
?>
Alternatively, you could look at exporting the node's XML and reimporting it into a DOMDocumentFragment, but I'd have to experiment to know for sure.

Categories