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");
Related
$var1 = new SimpleXMLElement('CSVXML/xvar.xml', null, true);
$var2 = new SimpleXMLElement('CSVXML/yvar.xml', null, true);
let's say I get variables from two diffrents XML files, in the first XML files
<Number>3698</Number>
<InternalNumber>1</InternalNumber>
<Name>Bob</Name>
<Number>3500</Number>
<InternalNumber>2</InternalNumber>
<Name>Mike</Name>
<Number>2775</Number>
<InternalNumber>3</InternalNumber>
<Name>Dan</Name>
in the second XML I get the followings
<player>3698</player>
<group>A</group>
I do this
$varID = $var1->Number;
$varnumber = $var2->player;
if ($varID == $varnumber ){
echo '$var1->InternalNumber';
}
is this possible ?
I simply want to put out a variable, is A for XML! = B from XML2, is there anyway possible to do that?
I found this working fine. tested link
<?php
$str = <<<XML
<items>
<item>
<Number>3698</Number>
<InternalNumber>1</InternalNumber>
<Name>Bob</Name>
</item>
<item>
<Number>3500</Number>
<InternalNumber>2</InternalNumber >
<Name>Mike</Name>
</item>
<item>
<Number>2775</Number>
<InternalNumber>3</InternalNumber>
<Name>Dan</Name>
</item>
</items>
XML;
$str2 = <<<XML
<item>
<player>3698</player>
<group>A</group>
</item>
XML;
$da = new SimpleXMLElement($str2);
$varnumber = $da->player;
$data = new SimpleXMLElement($str);
foreach ($data->item as $item)
{
$this_number = $item->Number;
//echo $this_number."-".$item->InternalNumber."-".$varnumber."\n";
if((int)$this_number == (int)$varnumber ){
$this_internalnumber = $item->InternalNumber;
echo $this_internalnumber."\n";
}
else{
echo "No Match found \n";
}
}
Hope this helps.
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"}
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();
}
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...
}
}
}
$books = new DOMDocument();
$books->load( 'books.xml' );
$id = $_GET['id'];
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<results><course>'.$id.'</course>';
$xml .= '<books>';
$items = $books->getElementsByTagName("item");
foreach( $items as $item )
{
$item_id = $item->getAttribute("id");
if($id == $item_id){
....
}
}
$xml .= '</books></results>';
header ("Content-Type:text/xml");
echo ($xml);
My code above will return me :
<results>
<course>1111</course>
<books>
<isbn id="0134582667" common="10" before="4" same="1" after="5" total="248"/>
.....
</books>
</results>
This page contains the following errors:
error on line 772 at column 12: XML declaration allowed only at the
start of the document Below is a rendering of the page up to the first
error.
You could get you items into an array, and then sort the array, something like this...
books.xml
<?xml version="1.0"?>
<results>
<course>1111</course>
<books>
<isbn id="0134582667" common="10" before="4" same="1" after="5" total="248">248</isbn>
<isbn id="0134582667" common="10" before="4" same="1" after="5" total="3">3</isbn>
<isbn id="0134582667" common="10" before="4" same="1" after="5" total="224">224</isbn>
<isbn id="0134582667" common="10" before="4" same="1" after="5" total="25">25</isbn>
</books>
</results>
test.php
$books = new DOMDocument();
$books->load( 'books.xml' );
$items = $books->getElementsByTagName("isbn");
$nodeArray = array();
foreach ($items as $item) {
$nodeArray[$item->getAttribute('total')] = $item;
}
ksort($nodeArray);
echo "<pre>". print_r($nodeArray, true) ."</pre>";