Please check this code, taken from here
<?php
$xml = <<<XML
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
</book>
<book>XML basics</book>
</books>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
foreach($xmlIterator->getChildren() as $name => $data) {
echo "The $name is '$data' from the class " . get_class($data) . "\n";
}
}
?>
How to get values from all book nodes additional "XML basics" (in second book node) using iterator?
This will give you XML basics as output.
<?php
$xml = <<<XML
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
</book>
<book>XML basics</book>
</books>
XML;
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
$xmlIterator->next();
echo $xmlIterator->getChildren();
}
?>
[:edit]
it will display both of books
$xmlIterator = new SimpleXMLIterator($xml);
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
foreach($xmlIterator->getChildren() as $name => $data) {
echo "$data<br />";
break;
}
echo $xmlIterator->getChildren();
}
Related
I have an XML file in which one child has two categories, but with the same name. I want to add one title to each one. How can we do it in PHP?
This is my XML
<root>
<result>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_two </category>
</categories>
</node>
<node>
<title> Some Title Name</title>
<categories>
<category> categor_one </category>
<category> categor_tree </category>
</categories>
</node>
</result>
</root>
But I want to obtain this
<root>
<result>
<node>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
<category1>categor_one///categor_tree</category1>
</node>
</result>
</root>
I managed to impement a function that only gets correctly the category, but if the title is the same it doesn't work as it just creates a new one.
function solve_something($xml, $destination)
{
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$items = json_decode($json, TRUE);
$products = array();
$product_data = array();
foreach($items['result']['node'] as $item){
$product_data['title'] = $item['title'];
foreach ($item['categories'] as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
} else {
$product_data['category'] = $category;
}
}
$products[] = $product_data;
unset($product_data);
}
$path = createXML($products, $destination);
return $path;
}
function createXML($data, $destination)
{
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($data as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
foreach ($product as $key1 => $val) {
if (!empty($val)) {
if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount') {
$productA->appendChild($xmlDoc->createElement($key1, $val));
} else {
$ProductKey = $productA->appendChild($xmlDoc->createElement($key1));
$ProductKey->appendChild($xmlDoc->createCDATASection($val));
}
}
}
}
$xmlDoc->formatOutput = true;
fn_rm($destination);
$xmlDoc->save($destination);
return $destination;
}
The output of my code is something like this
<root>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_two </category>
</product>
<product>
<title> Some Title Name</title>
<category>categor_one///categor_tree</category>
</product>
</root>
If you want to keep the data together with the same title, one approach could be to collect that data upfront by using the title as an array key (if it is a valid array key)
When you create the xml, you have the title in the outer foreach loop as the key, and in the inner foreach you can create elements using implode.
Note that in your code you started using product so I took that as a node name.
Example code, which you could use in your code:
$products = array();
$product_data = array();
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
foreach ($xml->result->node as $node) {
$product_data['title'] = (string)$node->title;
foreach($node->categories as $category) {
if (is_array($category)) {
$product_data['category'] = implode('///', $category);
continue;
}
$product_data['category'] = (array)$category->category;
}
$products[(string)$node->title][] = $product_data;
}
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($products as $key => $product) {
$productA = $root->appendChild($xmlDoc->createElement('product'));
$productA->appendChild($xmlDoc->createElement("title", $key));
for ($i = 0; $i < count($product); $i++) {
$productA->appendChild($xmlDoc->createElement("category" . $i, implode("///", $product[$i]["category"])));
}
}
$xmlDoc->formatOutput = true;
echo $xmlDoc->saveXML();
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product>
<title> Some Title Name</title>
<category0> categor_one /// categor_two </category0>
<category1> categor_one /// categor_tree </category1>
</product>
</root>
Php demo
I am trying to add another element to to my xml file.
Here is what it generates right now.
flipsnack>
<title>Book 1</title>
<date>6-6-2016</date>
<link>google.com</link>
<embed>this is hetml code</embed>
<order>1</order>
<show>1</show>
<flipsnack>
<title/>
<link>hotmail.com</link>
<embed>html code all the way</embed>
<order>2</order>
<postdate/>
<show>1</show>
</flipsnack>
</flipsnack>
Here is what i think it should be....
<?xml version="1.0" encoding="UTF-8"?>
<flipsnack>
<book>
<title>Book 1</title>
<date>6-6-2016</date>
<link>google.com</link>
<embed>this is hetml code</embed>
<order>1</order>
<show>1</show>
</book>
<book>
<title>Book 1</title>
<date>6-6-2016</date>
<link>google.com</link>
<embed>this is hetml code</embed>
<order>1</order>
<show>1</show>
</book>
</flipsnack>
Here is my php code:
$title=$_POST["post"];
$date=$_POST["date"];
$link=$_POST["link"];
$html=$_POST["html"];
$order=$_POST["order"];
$show=$_POST["show"];
$xml = simplexml_load_file("db.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$newItem = $sxe->addChild("flipsnack");
$newItem->addChild("title", $title);
$newItem->addChild("link", $link);
$newItem->addChild("embed", $html);
$newItem->addChild("order", $order);
$newItem->addChild("postdate", $postdate);
$newItem->addChild("show", $show);
$sxe->asXML("db.xml");
I dont quite under stand how i get from what its doing now to what i want it to do? Could anyone give me some direction?
See the code and take the idea what you are doing wrong.
<?php
$title = 'Book 1';
$link = 'google.com';
$order = 1;
$postdate = 'some postdate';
$show = 'some show';
$sxe = new SimpleXMLElement ( '<flipsnack/>' );
for($i = 0; $i < 2; $i ++) {
$newItem = $sxe->addChild ( "book" );
$newItem->addChild ( "title", $title );
$newItem->addChild ( "link", $link );
$newItem->addChild ( "order", $order );
$newItem->addChild ( "postdate", $postdate );
$newItem->addChild ( "show", $show );
}
echo $sxe->asXML ();
?>
Output
<?xml version="1.0"?>
<flipsnack>
<book>
<title>Book 1</title>
<link>google.com</link>
<order>1</order>
<postdate>some postdate</postdate>
<show>some show</show>
</book>
<book>
<title>Book 1</title>
<link>google.com</link>
<order>1</order>
<postdate>some postdate</postdate>
<show>some show</show>
</book>
</flipsnack>
Here is what i was trying to do.
$title=$_POST["title"];
$date=$_POST["date"];
$link=$_POST["link"];
$html=$_POST["html"];
$order=$_POST["order"];
$show=$_POST["show"];
$xml = simplexml_load_file("db.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$newItem = $sxe->addChild("book");
$newItem->addChild("title", $title);
$newItem->addChild("date", $date);
$newItem->addChild("link", $link);
$newItem->addChild("embed", $html);
$newItem->addChild("order", $order);
$newItem->addChild("show", $show);
$sxe->asXML("db.xml");
I would like to generate files from xml, I try this but it not working:
<?php
$file0 = "fle0.php";
$file1 = "fle1.php";
$file2 = "fle2.php";
$file3 = "fle3.php";
$file4 = "fle4.php";
$file5 = "fle5.php";
$file6 = "fle6.php";
$file7 = "fle7.php";
$file8 = "fle8.php";
$file9 = "fle9.php";
$html = "";
$url = "http://website.com/file.xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$link = $xml->books[$i]->link;
$title = $xml->books[$i]->title;
$html .= "$title";
file_put_contents($file[$i], $html);
}
?>
Can I use file_put_contents in for loop?
Here is the XML file:
<response>
<books>
<link>http:/www.website.com/linktobook/1.html</link>
<title>Book Title 1</title>
<image>http:/www.website.com/linktobook/1.jpg</image>
</books>
<books>
<link>http:/www.website.com/linktobook/2.html</link>
<title>Book Title 2</title>
<image>http:/www.website.com/linktobook/2.jpg</image>
</books>
<books>
<link>http:/www.website.com/linktobook/3.html</link>
<title>Book Title 3</title>
<image>http:/www.website.com/linktobook/3.jpg</image>
</books>
</response>
It is working without loop when I use $xml->books[0]->link; and $xml->books[1]->link; etc.
Thanks to: Jack
The answer is: change $file[$i] to ${"file$i"}
I have a xml file such as this:
<?xml version="1.0"?>
<datas>
<books>
<book>
<id>1</id>
<title>PHP Undercover</title>
<author>Wiwit Siswoutomo</author>
</book>`enter code here`
<book>
<id>2</id>
<title>PHP Enterprise</title>
<author>Wiwit Siswoutomo</author>
</book>
</books>
</datas>
and now i wana to replace a special tag in this way:
search in xml file and if it has a PHP Enterprise ,find it and replace it by 'new title'.
What should i do?
TNX
You can parse the document with SimpleXML:
$datas = new SimpleXMLElement("my.xml");
foreach ($datas->books->book as $book) {
if (preg_match('/PHP Enterprise/', $book->title) {
$book->title = "new title";
}
}
$datas->asXML("my.xml");
This script will help you!! Try this :)
<?php
function replace_Special_Str($mystring)
{
//Load file
$filename="./myfile.xml";
if (!file_exists($filename)) { echo "There is not a myfile.xml file in the directory."; exit;}
$xml = simplexml_load_file($filename);
//search and replace particular node by book title
$node = $xml->xpath('/datas/books[title="' . $mystring. '"]');
if(sizeof($node) > 0)
{
$node[0]->title = 'My Title';
}
$xml->asXML('./aucstatus.xml');
}
?>
OR Try This,
<users>
<name>John</name>
<address>My Address</address>
<zipcode>12345</zipcode>
<city>My City</city>
<phone>555 1234-4321</phone>
</users>
PHP FILE
fopen('users.xml');
while ($users->read()) {
switch ($users->nodeType) {
case (XMLReader::ELEMENT):
if ($users->localName == "users") {
$node = $reader->expand();
$dom = new DomDocument();
$n = $dom->importNode($node,true);
$dom->appendChild($n);
$simple_xml = simplexml_import_dom($n);
$id = $simple_xml['id'];
$name = $simple_xml->name;
$address = $simple_xml->address;
// Custom code insert, update, whatever...
}
}
}
I am trying to edit some XML with PHP. Currently the XML looking something like:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Main Title</title>
<link>http://exmaple.com</link>
<description> blahblahblah </description>
<language>en</language>
<item>
<title>Tite1</title>
<link>http://www.example.com (THIS IS WHAT I WANT)</link>
<description>blah blah blah</description>
</item>
.
.
.
</channel>
</rss>
I've tried to access the 2nd level link but my code only changes the first Link node value. Here is the code:
$xml->load('http://www.google.com/doodles/doodles.xml');
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item(0);
$2ndlevellinknode->nodeValue = $newvalue;
Any suggestions? Also is it possible to use this line of code in a for loop like this
for ($i = 0; $i <= 20; $i++) {
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item($i);
$2ndlevellinknode->nodeValue = $newvalue;
}
this should give you an idea.
$f = simplexml_load_file('test.xml');
print $f->channel->title . "\n";
print $f->channel->link . "\n";
print $f->channel->description . "\n";
foreach($f->channel->item as $item) {
print $item->title . "\n";
}