Generate files from xml for each node - php

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"}

Related

PHP to create XML File not generating properly

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");

how can i load a xml, and save it with php?

hey guys I'm trying to parse a xml(feeds) and save it with php?(I'm halfway),.i already achieve to load the xml and display just the data i need to display,.now how can i save the xml?
this is my code :
<?php
$html = "";
$url = "http://www.conciencia.net/rss.aspx";
$xml = simplexml_load_file($url);
$channel_title = $xml->channel->title;
$channel_link = $xml->channel->link;
$managingEditor = $xml->channel->managingEditor;
$channel_description = $xml->channel->description;
$lbd = $xml->channel->lastBuildDate;
$html .= "<br/>$channel_title";
$html .= "<br/>$channel_link";
$html .= "<br/>$managingEditor";
$html .= "<br/>$lbd";
$html .= "<br/>$channel_description<br/>";
for($i = 0; $i < 7; $i++){
$pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$guid = $xml->channel->item[$i]->guid;
$author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;
$url0 = $xml->channel->item[$i]->enclosure->attributes()->url;
for($ii = 0; $ii < 2; $ii++){
$url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
}
$html .= "<br/>$pubDate";
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "<br/>$guid";
$html .= "<br/>$author";
$html .= "<br/>$description";
$html .= "<br/>$url0";
$html .= "<br/>$url";
$html .= "<br/>$link<br/>";
}
echo $html;
?>
that code is working good, loading the tags from the xml i just want to display,
what i want to do after that is create a xml(as is below structured) and save the data.
how can i achieve that ?
myxml.xml
<channel>
<title>$channel_title</title>
<link>$channel_link</link>
<managingEditor>$managingEditor</managingEditor>
<channel_description>$channel_description</channel_description>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
</channel>
Well you can simly use normal string concatenation to write the xml file(alternatively you could spend time doing it the hard way with an xml object, which some might consider more proper).
Next you open a file with fopen() in write mode. if the file doesn't exist, it will be created. make sure you have permission to write to that directory, or the file will not be written(and if error reporting is strict, a fatal error will be thrown, else just a warning). second, you use fwrite() to write to the file.for more information, read the manual on filesystem functions
$file = '/path/to/mynewxmlfile.xml';
$data =
'
<?xml version="1.0"?>
<channel>
<title>'.$channel_title.'</title>
<link>'.$channel_link.'</link>
<managingEditor>'.'$managingEditor.</managingEditor>
<channel_description>'.$channel_description.'</channel_description>
';
for($i = 0; $i < 7; $i++){
$pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$guid = $xml->channel->item[$i]->guid;
$author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;
$url0 = $xml->channel->item[$i]->enclosure->attributes()->url;
for($ii = 0; $ii < 2; $ii++){
$url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
}
$data .=
'
<item>
<title>'.$title.'</title>
<guid>'.$guid.'</guid>
<author>'.$author.'</author>
<description>'.$description.'</description>
<enclosure0>'.$url0.'</enclosure0>
<enclosure1>'.$url.'</enclosure1>
</item>
';
}
$data .= '</channel>';
fwrite(fopen($file,'w'),$data);
Alternatively, you could use simplexml_load_string() to load the string as xml, then use asXml() to write it to disk, like so:
$data = simplexml_load_string($data);
$data->asXml($file);
Read the docs http://php.net/manual/en/simplexmlelement.asxml.php
$xml->asXml('rss.xml');
Edit
and if you want to create a new xml there is an answer https://stackoverflow.com/a/143192/1742977

Get value form SimpleXMLIterator if has no children

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();
}

How to replace a special xml tag by using php

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...
}
}
}

PHP access XML node element

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";
}

Categories