PHP simpleXML: Read XML, add node and save - php

I'm having some problems with adding a xml node to its parent.
I receive variables $cat, $title and $isbn.
I want to parse $title and $isbn to an XML node and add it to the right categorie ($cat).
die(var_dump($parent)); --> returns NULL, so the biggest problem (I think) is that I can't figure out how to add my node to the right parent because I cant get it identified.
Any suggestions?
The XML file:
<?xml version="1.0"?>
<books version="1.0">
<categorie name="catone" id="100">
<book title="1_WS2012" isbn="isbnone" />
<book title="1W2012DHCP" isbn="ibsntwo" />
</categorie>
<categorie title="cattwo" id="101">
<book title="2W2008R2DC" isbn="isbnthree" />
</categorie>
<categorie title="catthree" id="103">
<book title="3SBS" isbn="isbnfout=" />
</categorie>
</books>
The Code:
//Get variables
$cat = "catone";
$title = "testtitle";
$isbn = "testisbn";
$xmlDoc = simplexml_load_file("books.xml");
$parent = null;
//Construct node
$childstring = "<book></book>";
$child = new SimpleXMLElement($childstring);
$child->addAttribute('title', $title);
$child->addAttribute('isbn', $isbn);
//This works (results in <book title="testtile" isbn="testisbn" />)
//Add node to correct parent
for ($i=0; $i <= sizeof($xmlDoc->categorie) -1; $i++) {
//The condition does also work
if (strtoupper($xmlDoc->categorie[$i]->attributes()->name) == strtoupper($cat))
{
//I'm stuck here
$parent = $xmlDoc->categorie[$i]->attributes()->xpath('/object/data[#type="me"]');;
$xmlDoc->$parent->addChild($child);
}
}
//Write file
file_put_contents("books.xml", $xmlDoc->asXML());
Desired result:
<books version="1.0">
<categorie name="catone" id="100">
<book title="1_WS2012" isbn="isbnone" />
<book title="1W2012DHCP" isbn="ibsntwo" />
<book title="testtitle" isbn"testisbn" />
</categorie>
<categorie title="cattwo" id="101">
<book title="2W2008R2DC" isbn="isbnthree" />
</categorie>
<categorie title="catthree" id="103">
<book title="3SBS" isbn="isbnfout=" />
</categorie>
</books>

First, use xpath to select the parent. xpath is like SQL for XML:
$xml = simplexml_load_string($x); // assume XML in $x
$parent = $xml->xpath("/books/categorie[#name = 'catone']")[0];
Note: The above code requires PHP >= 5.4 for the [0] at the end of line 2. (1)
Now, add the new <book> and its attributes:
$new = $parent->addChild("book","");
$new->addAttribute("title", "testtitle");
$new->addAttribute("isbn", "testisbn");
see it working: https://eval.in/131009
(1) if you are on PHP < 5.4, either update or do:
$parent = $xml->xpath("/books/categorie[#name = 'catone']");
$parent = $parent[0];

Related

Selecting Attribute and listing Value base on NODE Value using Xpath

How do i get All Attribute Value ID of every node FLOWER?
<Files>
<data id="1">
<Type>Flower</Type>
</data>
<data id="2">
<Type>Flower</Type>
</data>
<data id="3">
<Type>Flower</Type>
</data>
<data id="4">
<Type>Flower</Type>
</data>
</Files>
In mysql case it will be like SELECT id from Files WHERE Type="Flower"
How do i code xpath for this condition?
and List it using SimpleXML within option box.
<select>
<?php
foreach ($type as $id) {
echo '<option value="'.$id.'">'.$id.'</option>';
}
?>
</select>
To get all #id attribute's values try
/Files/data[normalize-space(Type) = 'Flower']/#id
'//data[(./Type/text()="Flower")]/#id'
Your XML is invalid, the closing root element does not match and the Type elements are closed as type. XML is case sensitive.
Xpath works uses location paths and conditions a location path is a hierarchical path to the element from the current context. They return a list of nodes. The list can be filtered using conditions.
SimpleXMLElement objects have a method xpath() to execute an expression in the context of the associated node.
$xml = <<<'XML'
<Files>
<data id="1">
<type>Flower</type>
</data>
<data id="2">
<type>Flower</type>
</data>
<data id="3">
<type>Flower</type>
</data>
<data id="4">
<type>Flower</type>
</data>
</Files>
XML;
$files = new SimpleXMLElement($xml);
$target = new SimpleXMLElement('<select/>');
foreach ($files->xpath('data[type = "Flower"]') as $data) {
echo '.';
$option = $target->addChild('option', $data['id']);
$option['value'] = $data['id'];
}
echo $target->asXml();
You should not create you XML as text. Use an XML Api for it.
DOM is more specific and powerful. For example you can serialize the created DOM as HTML.
$source = new DOMDocument();
$source->loadXml($xml);
$xpath = new DOMXpath($source);
$target = new DOMDocument();
$select = $target->appendChild($target->createElement('select'));
foreach ($xpath->evaluate('/Files/data[type = "Flower"]') as $data) {
$option = $select->appendChild($target->createElement('option'));
$option->setAttribute('value', $data->getAttribute('id'));
$option->appendChild($target->createTextNode($data->getAttribute('id')));
}
echo $target->saveHtml($select);
This is how i used the answer Feel free to use the code if you like.
Thanks!!
<?php
//I have used 2 given answer as example on how i used it. Feel Free to use the code below
$type = $_GET['type'];
if(file_exists("xml/data.xml")) {
$xml = simplexml_load_file('xml/data.xml') or die("Data Missing"); }
<!-- Code Example 1 -->
$ids = $xml->xpath('//data[(./Type/text()="'.$type.'")]/#id');
<!-- Code Example 2 -->
$idx = $xml->xpath('/Files/data[normalize-space(Type) = "'.$type.'"]/#id');
?>
<!-- Example 1 -->
<select>
<?php
//echo $ids[0];
foreach ($ids as $id) {
echo '<option value="'.$id[0].'">'.$id[0].'</option>';
}
?>
</select>
<!-- Example 2 -->
<select>
<?php
//echo $ids[0];
foreach ($idx as $id2) {
echo '<option value="'.$id2[0].'">'.$id2[0].'</option>';
}
?>
</select>
<a href="logout.php">Logout
</a>

How to get number of nodes inside a xml file?

Having a xml file so formed:
<book id="1">
<chapter id="1">
...
</chapter>
<chapter id="2">
...
</chapter>
<chapter id="3">
...
</chapter>
</book>
How i can count nodes inside ... ?
In example above, the function should be turn 3 as result becouse present three nodes.
NOTE:
I access to ... so:
$xpath = new DOMXPath($filename);
$book = $xpath->query("//book[#id='1']")->item(0);
You can do as:
Refer: http://de2.php.net/manual/en/book.dom.php
$dom->getElementsByTagName('chapter')->length;
Unless you can use this:
$xpath->evaluate('count(//book/chapter');
Try this code
function xml2array($xmlObject, $out = array()) {
foreach ((array) $xmlObject as $index => $node)
$out[$index] = ( is_object($node) ) ? xml2array($node) : $node;
return $out;
}
$a = '<book>
<chapter id = "1">
...
</chapter>
<chapter id = "2">
...
</chapter>
<chapter id = "3">
...
</chapter>
</book>';
$a = simplexml_load_string($a);
$array=xml2array($a);
echo count($array['chapter']);
EDIT
Using DOMXpath try this
echo $book = $xpath->query("//book[#id='1']/chapter")->length;

Parsing throuth complex xml with PHP

I have a xml file with structure like this:
<categories>
<category>
<id></id>
<name></name>
</category>
...
</categories>
<products>
<product>
<id></id>
<name></name>
</product>
...
</products>
<params>
<param>
<id></id>
<name></name>
</param>
...
</params>
<product_params>
<product_param>
<param_id></param_id>
<product_id></product_id>
</product_param>
...
</product_params>
How do I display product nodes, right category (that matches id) and all params for that product?
I tried doing something like this:
$xml = simplexml_load_file('file.xml');
$products = $xml->products;
$product_params = $xml->product_params->product_param;
foreach ($products->product as $product) {
echo '<p>'.$product->name.'</p>';
for($i=0; $i < count($product_params) ;$i++) {
if($product->id == $product_params[$i]->product_id) {
}
}
}
The file is too big and script crashes. Plus with my "solution" I would need at least one more loop nested in there.

Count how many children are in XML with PHP

i know a few about php, so sorry for the question:
i have this file xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<alert>
<status> </status>
<nothing> </nothing>
<info>
<area>
</area>
</info>
<info>
<area>
</area>
</info>
<info>
<area>
</area>
</info>
</alert>
i must do a for loop and inside a "foreach" for each
The problem is that i'm not sure what is a way to know how many times i had to repeat a for loop. Because in this file xml (that is an example) i don't know how many are
Is good if:
$url = "pathfile";
$xml = simplexml_load_file($url);
$numvulcani = count($xml->alert->info); // is good ?
for ($i = 0; $i <= $numvulcani; $i++) {
foreach ($xml->alert->info[$i] as $entry) {
$area = $entry->area;
}
}
is true ?
sorry for bad english
You need to use SimpleXMLElement::count function for this — It counts the children of an element.
<?php
$xml = <<<EOF
<people>
<person name="Person 1">
<child/>
<child/>
<child/>
</person>
<person name="Person 2">
<child/>
<child/>
<child/>
<child/>
<child/>
</person>
</people>
EOF;
$elem = new SimpleXMLElement($xml);
foreach ($elem as $person) {
printf("%s has got %d children.\n", $person['name'], $person->count());
}
?>
The output will be as follows :
Person 1 has got 3 children.
Person 2 has got 5 children.
Also take a look at this link : xml count using php
Try replacing foreach ($xml->alert->info[$i] as $entry) with:
foreach ($xml->alert->info[$i] as $j => $entry)
The current item index will be $j
You're perhaps overcomplicating this a bit as it's new to you.
First of all, you don't need to reference the alert root element like $xml->alert because the SimpleXMLElement named by the variable $xml represents that document element already.
And second, you don't need to count here, you can just foreach directly:
foreach ($xml->info as $info) {
echo ' * ', $info->asXML(), "\n";
}
This iterates over those three info elements that are children of the alert element.
I recommend the Basic SimpleXML usage guide in the PHP manual for a good start with SimpleXML.

Parsing XML data using php to put into mysql database

I have been asked to parse a simple file which is stored as an XML file, the data is to be then put into a mysql database.
However I have absolutely no clue what to do and after looking online all the examples given seem either too complicated for my problem or not the right solution. The XML file looks like this:
<shop>
<products>
<product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
<product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
</products>
<stocks>
<stock id="1" amount="242" price="pounds" />
<stock id="2" amount="11" price="pounds" />
</stocks>
I've tried looking at SimpleXML and I think that's the direction I have to go but I just have no idea.
Any help or pointers would be great.
I personally like the normal XMl formatting so I changed it since its a bit more readable but this is how you can use it:
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<shop>
<products>
<product>
<id>1</id>
<name>Cornetto</name>
<price>1.20</price>
<description>Traditional Cornetto</description>
</product>
<product>
<id>2</id>
<name>Smarties</name>
<price>1.00</price>
<description>Smarties Icecream</description>
</product>
</products>
<stocks>
<stock>
<id>1</id>
<amount>242</amount>
<price>pounds</price>
</stock>
<stock>
<id>2</id>
<amount>11</amount>
<price>pounds</price>
</stock>
</stocks>
</shop>
XML;
Handling part:
$xml = new SimpleXMLElement($xmlstr);
echo 'single value: <br />';
echo $xml->products->product[0]->id; // get single value
echo '<br /><br />';
//Loop trough multiple products
echo 'multiple values: <br />';
foreach($xml->products->product as $product)
{
echo $product->id.' - ';
echo $product->name.' - ';
echo $product->price.' - ';
echo $product->description;
echo '<br/>';
}
Assuming the file is called data.xml
$string = file_get_contents('data.xml') reads the entire file into $string.
$xml = new SimpleXMLElement($string); parses that string, and converts it into an object tree similar to the actual document. So if that's the document -
<root>
<b>
<c>first</c>
<c>second</c>
</b>
</root>
The SimpleXMLElement object would be used like:
$xml->b // gets all children of b (c[0] and c[1])
print $xml->b->c[0] // gets the first c, will print "first"
You can use for example SimpleXMLElement and xpath
<?php
$xmlStr = <<<EOF
<?xml version="1.0"?>
<shop>
<products>
<product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
<product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
</products>
<stocks>
<stock id="1" amount="242" price="pounds" />
<stock id="2" amount="11" price="pounds" />
</stocks>
</shop>
EOF;
$xml=new SimpleXMLElement($xmlStr);
// get product line with xpath for example
$products=$xml->xpath("/shop/products/product");
if ($products) {
// loop over each product node
foreach ($products as $product) {
// do whatever you want with the data
echo("id=>".$product["id"].", name=>".$product["name"]."<br/>");
}
}
// same for stock
// get product line with xpath for example
$stocks=$xml->xpath("/shop/stocks/stock");
if ($stocks) {
// loop over each product node
foreach ($stocks as $stock) {
// do whatever you want with the data
echo("id=>".$stock["id"].", amount=>".$stock["amount"]."<br/>");
}
}
?>
$xml = simplexml_load_file($filename);
foreach($xml->product as $product) {
foreach($product->attributes() as $name => $attribute) {
echo "$name = $attribute";
}
}
$xml = simplexml_load_file($filename);
foreach($xml->products->product as $not)
{
foreach($not->attributes() as $a => $b)
{
echo $a,'="',$b,"\"<br />";
}
}

Categories