This question already has answers here:
PHP SimpleXML: insert node at certain position
(2 answers)
Closed 8 years ago.
I'm trying to add a comment list to a page using an xml file. I'd like to list the comments most recent first, so when a new comment is added, I'd like to add it to the start of the xml. addChild appends to the end, so that's no good, and I can't get my head around the the DOMNode insert_before method, as I want to add it at the start before every other occurrence of a child (and I can't find an example anywhere that does this - weird).
xml file looks like;
<comments>
<comment>
<date>20130625</date>
<name>Jocky Wilson</name>
<text>Something about darts presumably</text>
</comment>
<comment>
<date>20130622</date>
<name>Jacky Wilson</name>
<text>It was reet petite etc</text>
</comment>
</comments>
I create the file initially with;
<?php
$xmlData = "< load of xml etc...";
$xml = new SimpleXMLElement($xmlData);
file_put_contents("comments.xml", $xml->asXML());
?>
And that works fine. Any suggestions at all gratefully received.
As an alternative to the solutions mentioned in the comments:
use addChild, let it add the node wherever it wants to, sort it by <date> and echo it:
$xml = simplexml_load_string($x); // assume XML in $x
$comments = $xml->xpath("//comment");
$field = 'date';
sort_obj_arr($comments, $field, SORT_DESC);
var_dump($comments);
// function sort_obj_array written by GZipp, see link below
function sort_obj_arr(& $arr, $sort_field, $sort_direction) {
$sort_func = function($obj_1, $obj_2) use ($sort_field, $sort_direction) {
if ($sort_direction == SORT_ASC) {
return strnatcasecmp($obj_1->$sort_field, $obj_2->$sort_field);
} else {
return strnatcasecmp($obj_2->$sort_field, $obj_1->$sort_field);
}
};
usort($arr, $sort_func);
}
see GZipp's original function: Sorting an array of SimpleXML objects
Related
This question already has answers here:
How to use XMLReader in PHP?
(7 answers)
Closed 6 years ago.
PHP developers here ??
I have a PHP function who parse an xml file (using DOMDocument, i'm proficien with this tool). I want to do the same with XMLReader, but i don't understand how XMLReader works...
I want to use XMLReader because it's a light tool.
Feel free to ask me others questions about my issue.
function getDatas($filepath)
{
$doc = new DOMDocument();
$xmlfile = file_get_contents($filepath);
$doc->loadXML($xmlfile);
$xmlcars = $doc->getElementsByTagName('car');
$mycars= [];
foreach ($xmlcars as $xmlcar) {
$car = new Car();
$car->setName(
$xmlcar->getElementsByTagName('id')->item(0)->nodeValue
);
$car->setBrand(
$xmlcar->getElementsByTagName('brand')->item(0)->nodeValue
);
array_push($mycars, $car);
}
return $mycars;
}
PS : I'm not a senior PHP dev.
Ahah Thanks.
This is a good example from this topic, I hope it helps you to understand.
$z = new XMLReader;
$z->open('data.xml');
$doc = new DOMDocument;
// move to the first <product /> node
while ($z->read() && $z->name !== 'product');
// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'product')
{
// either one should work
//$node = new SimpleXMLElement($z->readOuterXML());
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
// now you can use $node without going insane about parsing
var_dump($node->element_1);
// go to next <product />
$z->next('product');
}
XMLReader does not, as far as I can tell, have some equivalent way of filtering by an element name. So the closest equivalent to this would be, as mentioned in rvbarreto's answer, to iterate through all elements using XMLReader->read() and grabbing the info you need when the element name matches what you are wanting.'
Alternatively, you might want to check out SimpleXML, which supports filtering using XPath expressions, as well as seeking to a node in the XML using the element structure like they are sub-objects of the main object. For instance, instead of using:
$xmlcar->getElementsByTagName('id')->item(0)->nodeValue;
You would use:
$xmlcar->id[0];
Assuming all of your car elements are at the first level of the XML document tree, the following should work as an example:
function getDatas($filepath) {
$carsData = new SimpleXMLElement($filepath, NULL, TRUE);
$mycars = [];
foreach($carsData->car as $xmlcar) {
$car = new Car();
$car->setName($xmlcar->id[0]);
$car->setBrand($xmlcar->id[0]);
$mycars[] = $car;
}
}
This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
xml remove child node in php
(3 answers)
Closed 7 years ago.
The goal is to delete every XML child with the same parent that matches the id.
The problem I've encountered was that only the first XML child was removed. When I ran this file the output was "The id is: 1" and also "CarlCarlok". The script removed the first XML child with the attribute "1". Which also was proven when I checked the XML file afterwards where the comment with id "9" was removed. So..To resolve this issue i have to know:
How do I remove every XML child with the same parent?
Because now it only removes the first one it encounters.
<?php
require '../functions.php';
$id = $_GET['id'];
echo "The id is: " . $id . "\n";
$comment_xml_path = '../data/comments.xml';
$comments_xml_file = simplexml_load_file($comment_xml_path);
foreach ($comments_xml_file->comment as $c) {
echo $c->author;
if (strcmp($id, $c['parent']) == 0) {
$dom = dom_import_simplexml($c);
$dom->parentNode->removeChild($dom);
echo "ok";
}
save_xml($comment_xml_path, $comments_xml_file);
(save_xml() is a function for saving utilizing asXML that we made during class)
The XML file
<?xml version="1.0" encoding="UTF-8"?>
<comments>
<comment id="6" parent="3" date="20150317224217">
<author>Carl</author>
<cmt>3</cmt>
</comment>
<comment id="9" parent="1" date="20150312225112">
<author>Carl</author>
<cmt>dsa</cmt>
</comment>
<comment id="10" parent="1" date="20150356225256">
<author>Carl</author>
<cmt>2</cmt>
</comment>
<comment id="11" parent="1" date="20150357225257">
<author>Carl</author>
<cmt>2</cmt>
</comment>
</comments>
Here's a way with SimpleXML and xpath(), which is like SQL for XML:
$id = 1; // parent
$xml = simplexml_load_file("some/path/to/an/xml/file.xml");
$comments = $xml->xpath("/comments/comment[#parent='$id']");
foreach ($comments as $comment) unset($comment[0]);
echo $xml->asXML(); // show result
Comments:
line 3: the xpath-statement will select all <comment> where attribute parent = $id. The # in the statement means attribute. The condition is wrapped in []. The result is stored in $comments as an array of SimpleXML elements.
line 4: deleting each <comment> in the array.
If you want to remove all the children, I would instead loop over the childNodes and call removeChild().
Updated Code:
if (strcmp($id, $c['parent'])== 0) {
foreach ($c->childNodes as $child) {
$child->parentNode->removeChild($child);
}
}
This assumes you are using DOMDocument. However, you seem to be mixing this and SimpleXML.
I have a page in php where I have to parse an xml.
I have done this for example:
$hotelNodes = $xml_data->getElementsByTagName('Hotel');
foreach($hotelNodes as $hotel){
$supplementsNodes2 = $hotel->getElementsByTagName('BoardBase');
foreach($supplementsNodes2 as $suppl2) {
echo'<p>HERE</p>'; //not enter here
}
}
}
In this code I access to each hotel of my xml, and foreach hotel I would like to search the tag BoardBase but it doesn0t enter inside it.
This is my xml (cutted of many parts!!!!!)
<hotel desc="DESC" name="Hotel">
<selctedsupplements>
<boardbases>
<boardbase bbpublishprice="0" bbprice="0" bbname="Colazione Continentale" bbid="1"></boardbase>
</boardbases>
</selctedsupplements>
</occupancy></occupancies>
</hotel>
I have many nodes that doesn't have BoardBase but sometimes there is but not enter.
Is possible that this node isn't accessible?
This xml is received by a server with a SoapClient.
If I inspect the XML printed in firebug I can see the node with opacity like this:
I have also tried this:
$supplementsNodes2 = $hotel->getElementsByTagName('boardbase');
but without success
2 issues I can see from the get-go: XML names are case-sensitive, hence:
$hotelNodes = $xml_data->getElementsByTagName('Hotel');
Can't work, because your xml node looks like:
<hotel desc="DESC" name="Hotel">
hotel => lower-case!
As you can see here:
[...] names for such things as elements, while XML is explicitly case sensitive.
The official specs specify tag names as case-sensitive, so getElementsByTagName('FOO') won't return the same elements as getElementsByTagName('foo')...
Secondly, you seem to have some tag-soup going on:
</occupancy></occupancies>
<!-- tag names don't match, both are closing tags -->
This is just plain invalid markup, it should read either:
<occupancy></occupancy>
or
<occupancies></occupancies>
That would be the first 2 ports of call.
I've set up a quick codepad using this code, which you can see here:
$xml = '<hotel desc="DESC" name="Hotel">
<selctedsupplements>
<boardbases>
<boardbase bbpublishprice="0" bbprice="0" bbname="Colazione Continentale" bbid="1"></boardbase>
</boardbases>
</selctedsupplements>
<occupancy></occupancy>
</hotel>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$badList = $dom->getElementsByTagName('Hotel');
$correctList = $dom->getElementsByTagName('hotel');
echo sprintf("%d",$badList->lenght),
' compared to ',
$correctList->length, PHP_EOL;
The output was "0 compared to 1", meaning that using a lower-case selector returned 1 element, the one with the upper-case H returned an empty list.
To get to the boardbase tags for each hotel tag, you just have to write this:
$hotels = $dom->getElementsByTagName('html');
foreach($hotels as $hotel)
{
$supplementsNodes2 = $hotel->getElementsByTagName('boardbase');
foreach($supplementsNodes2 as $node)
{
var_dump($node);//you _will_ get here now
}
}
As you can see on this updated codepad.
Alessandro, your XML is a mess (=un casino), you really need to get that straight. Elias' answer pointed out some very basic stuff to consider.
I built on the code pad Elias has been setting up, it is working perfectly with me:
$dom = new DOMDocument;
$dom->loadXML($xml);
$hotels = $dom->getElementsByTagName('hotel');
foreach ($hotels as $hotel) {
$bbs = $hotel->getElementsByTagName('boardbase');
foreach ($bbs as $bb) echo $bb->getAttribute('bbname');
}
see http://codepad.org/I6oxkEOC
i've tried to find this out by myself before asking but cant really figure it out.
What I have is a loop, it's actually a loop which reads XML data with simplexml_load_file
Now this XML file has data which I want to read and put into an array.. a two dimensional array actually..
So the XML file has a child called Tag and has a child called Amount.
The amount is always differnt, but the Tag is usually the same, but can change sometimes too.
What I am trying to do now is:
Example:
This is the XML example:
<?xml version="1.0"?>
<Data>
<Items>
<Item Amount="9,21" Tag="tag1"/>
<Item Amount="4,21" Tag="tag1"/>
<Item Amount="6,21" Tag="tag2"/>
<Item Amount="1,21" Tag="tag1"/>
<Item Amount="6,21" Tag="tag2"/>
</Data>
</Items>
Now i have a loop which reads this, sees what tag it is and adds up the amounts.
It works with 2 loops and two different array, and I would like to have it all in one array in single loop.
I tried something like this:
$tags = array();
for($k = 0; $k < sizeof($tags); $k++)
{
if (strcmp($tags[$k], $child['Tag']) == 0)
{
$foundTAG = true;
break;
}
else
$foundTAG = false;
}
if (!$foundTAG)
{
$tags[] = $child['Tag'];
}
and then somewhere in the code i tried different variations of adding to the array ($counter is what counts the Amounts together):
$tags[$child['Tag']][$k] = $counter;
$tags[$child['Tag']][] = $counter;
$tags[][] = $counter;
i tried few other combinations which i already deleted since it didnt work..
Ok this might be a really noob question, but i started with PHP yesterday and have no idea how multidimensional arrays work :)
Thank you
this is how you can iterate over the returned object from simple xml:
$xml=simplexml_load_file("/home/chris/tmp/data.xml");
foreach($xml->Items->Item as $obj){
foreach($obj->Attributes() as $key=>$val){
// php will automatically cast each of these to a string for the echo
echo "$key = $val\n";
}
}
so, to build an array with totals for each tag:
$xml=simplexml_load_file("/home/chris/tmp/data.xml");
$tagarray=array();
// iterate over the xml object
foreach($xml->Items->Item as $obj){
// reset the attr vars.
$tag="";
$amount=0;
// iterate over the attributes setting
// the correct vars as you go
foreach($obj->Attributes() as $key=>$val){
if($key=="Tag"){
// if you don't cast this to a
// string php (helpfully) gives you
// a psuedo simplexml_element object
$tag=(string)$val[0];
}
if($key=="Amount"){
// same as for the string above
// but cast to a float
$amount=(float)$val[0];
}
// when we have both the tag and the amount
// we can store them in the array
if(strlen($tag) && $amount>0){
$tagarray[$tag]+=$amount;
}
}
}
print_r($tagarray);
print "\n";
This will break horribly should the schema change or you decide to wear blue socks (xml is extremely colour sensitive). As you can see dealing with the problem child that is xml is tedious - yet another design decision taken in a committee room :-)
This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Implementing condition in XPath [duplicate]
(2 answers)
Closed 9 years ago.
I am trying to parse out the value of a node I am referencing by one of its attributes. but I am not sure of the syntax
XML:
<data>
<poster name="E-Verify" id="everify">
<full_image url="e-verify-swa-poster.jpg"/>
<full_other url=""/>
</poster>
<poster name="Minimum Wage" id="minwage">
<full_image url="minwage.jpg"/>
<full_other url="spa_minwage.jpg"/>
</poster>
</data>
here is where I want to get the url value of full_image where poster id equal to minwage:
$xml = simplexml_load_file('PosterData.xml');
$main_url = $xml->full_image[name] where poster[id] = "minwage";
//something like that.
echo $main_url;
Result: minwage.jpg
If anyone has any resources that cover this stuff please share.
You should be able to use SimpleXMLElement::xpath() to run an xpath query on a simple XML document.
$xml = simplexml_load_file('PosterData.xml');
$main_url = $xml->xpath('name[#id="minwage"]/full_image')[0];
echo $main_url;
Simply loop the poster elements and remember to cast the attribute values to strings, since you want to compare them (and probably output them) as strings:
$xml = simplexml_load_file('PosterData.xml');
foreach ($xml->poster as $poster) {
if ((string) $poster['id'] == 'minwage') {
echo (string) $poster->full_image['url'];
}
}
You can use:
$object = simplexml_load_string('<data><poster name="E-Verify" id="everify"><full_image url="e-verify-swa-poster.jpg"/><full_other url=""/></poster><poster name="Minimum Wage" id="minwage"><full_image url="minwage.jpg"/><full_other url="spa_minwage.jpg"/></poster></data>');
foreach ($object as $value) {
echo "URL: ".$value->full_image->attributes();
echo "<br>";
}
Either use simplexml_load_file('Some external file.xml') if calling external file.