How to read data from this XML file with PHP? - php

Making a Connection
$Game_ID = $Game_Search->Game[$i]->id;
$Game_Info_URL = 'http://thegamesdb.net/api/GetGame.php?id='.$Game_ID;
$Game_Info_Output = simplexml_load_file($Game_Info_URL);
Retrieving Data Example
$Game_Info_Images = $Game_Info_Output->Game->Images;
For this question please refer to this URL where I would like to get the Game->Images-> Box Art Side A and Side B. How do I call this?
XML Doc (Just required Fields)
<Data>
<baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>
<Game>
<Images>
<boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart>
<boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart>
</Images>
</Game>
</Data>

XML Doc (Just required Fields)
<Data>
<baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>
<Game>
<Images>
<boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart>
<boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart>
</Images>
</Game>
</Data>
To read side="front" width="1530"... simply use;
boxart["Attribute_Name"]
Examples:
Game->Images->boxart[$b]["side"] // Gets the side value front/back
Game->Images->boxart[$b]["width"] // gets the width value
Game->Images->boxart[$b]["height"] // gets the height value
Game->Images->boxart[$b]["thumb"] // gets the thumb value

DomDocument and/or Xpath:
$dom = new DOMDocument();
$dom->load('http://thegamesdb.net/api/GetGame.php?id=90');
$xpath = new DOMXPath($dom);
$baseImgUrl = $xpath->query('//baseImgUrl')->item(0)->nodeValue;
$boxartBackSide = $xpath->query('//Game/Images/boxart[#side="back"]')
->item(0)->nodeValue;
$boxartFrontSide = $xpath->query('//Game/Images/boxart[#side="front"]')
->item(0)->nodeValue;

Related

How can I append new xml nodes to an existing .xml file with php class DOMDocument?

Let's say I have the following .xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
In this sample file, I'm trying to append new nodes <item> to node <root> after the last node <item>.
I'm trying to append newly created <item> nodes after the last <item> node in the <root> node in the .xml file.
<?php
$file = new DOMDocument;
$file->load("xml.xml");
$file->loadXML($file->saveXML());
$root = $file->getElementsByTagName('root')->item(0);
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = new DOMElement('item');
$item->appendChild(new DOMElement('name', $val));
$root->appendChild(item);
}
?>
But I'm getting an error:
Fatal error: Uncaught Error: Call to a member function appendChild() on null in C:\Users\pfort\Desktop\p.php:12
Stack trace:
#0 {main}
thrown in C:\Users\user_acer\Desktop\p.php on line 12
What am I doing wrong?
There's multiple issues with your example code. I will address the error you received first:
There is no element <terminy> in your example XML, so
$root = $file->getElementsByTagName('terminy')->item(0);
will return null. That's why you are receiving the
Call to a member function appendChild() on null
error at
$root->appendChild(item);
Also, item is a typo, because it's not a valid variable name (but a name for a non-existent constant); you meant $item.
I'm assuming "terminy" means something similar to "root" in your native language and that you actually meant to write
$root = $file->getElementsByTagName('root')->item(0);
By the way: if you want a reference to the root node of an XML document, you can also use $file->docomentElement.
However, there are other issues with your example code:
$file->load("xml.xml");
$file->loadXML($file->saveXML()); // why are you reloading it in this way?
The last line is unnecessary. You are reloading the same XML again. Is it for formatting purposes? If so, there's a better option available:
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->load("xml.xml");
Lastly: you cannot append children to a node that has not been associated with a document yet. So, to create a new item and associate it with the document, you either do (recommended):
// automatically associate new nodes with document
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
or (more cumbersome):
// import nodes to associate them with document
$item = $file->importNode(new DOMElement('item'));
$item->appendChild($file->importNode(new DOMElement('name', $val)));
So, putting it all together it becomes:
<?php
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
XML;
$file = new DOMDocument;
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->loadXML($xml); // (for demo purpose loading above XML) replace this with $file->load("xml.xml"); in your actual code
$root = $file->documentElement;
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
$root->appendChild($item);
}
echo $file->saveXML();
**PROBLEM SOLVED**
I lost too much time on this problem. The good news is, I already know how to get what I need. Here I offer a solution - for everyone who will need to solve the same problem.
Perhaps this solution will be useful for anyone who needs it.
<?php
// snippet of xml temple
$xml = <<<XML
<item date="%s" status="%s">
<name>%s</name>
</item>
XML;
// prepare snippet
$xmlSnippet = sprintf($xml, "2022-11-21", 0, "Foo Bar");
// new DOMDocument
$dom = new DOMDocument;
$dom->preserveWhiteSpace = 0;
$dom->formatOutput = 1;
// load of .xml file content and load to DOMDocument object
$file = simplexml_load_file("xml.xml");
$dom->loadXML($file->asXML());
// creating of fragment from snippet
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($xmlSnippet);
//append the snippet to the DOMDocument
// and save it to the xml.xml file
$dom->documentElement->appendChild($fragment);
$dom->save("xml.xml");
?>
Result:

How to get into into another child than 'firstChild' of XML file in PHP?

How can I get through another child of XML file than firstChild in PHP?
I have a code like this:
$root = $xmldoc->firstChild;
Can I simply get into second child or another?
A possible solution to your problem could be something like this. First your XML structure. You asked how to add an item node to the data node.
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<xmldata>
<data>
<item>item 1</item>
<item>item 2</item>
</data>
</xmldata>
XML;
In PHP one possible solution is the DomDocument object.
$doc = new \DomDocument();
$doc->loadXml($xml);
// fetch data node
$dataNode = $doc->getElementsByTagName('data')->item(0);
// create new item node
$newItemNode = $doc->createElement('item', 'item 3');
// append new item node to data node
$dataNode->appendChild($newItemNode);
// save xml node
$doc->saveXML();
This code example is not tested. Have fun. ;)

Hide XML declaration in files generated using PHP

I was tesing with a simple example of how to display XML in browser using PHP and found this example which works good
<?php
$xml = new DOMDocument("1.0");
$root = $xml->createElement("data");
$xml->appendChild($root);
$id = $xml->createElement("id");
$idText = $xml->createTextNode('1');
$id->appendChild($idText);
$title = $xml->createElement("title");
$titleText = $xml->createTextNode('Valid');
$title->appendChild($titleText);
$book = $xml->createElement("book");
$book->appendChild($id);
$book->appendChild($title);
$root->appendChild($book);
$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("mybooks.xml") or die("Error");
?>
It produces the following output:
<?xml version="1.0"?>
<data>
<book>
<id>1</id>
<title>Valid</title>
</book>
</data>
Now I have got two questions regarding how the output should look like.
The first line in the xml file '', should not be displayed, that is it should be hidden
How can I display the TextNode in the next line. In total I am exepecting an output in this fashion
<data>
<book>
<id>1</id>
<title>
Valid
</title>
</book>
</data>
Is that possible to get the desired output, if so how can I accomplish that.
Thanks
To skip the XML declaration you can use the result of saveXML on the root node:
$xml_content = $xml->saveXML($root);
file_put_contents("mybooks.xml", $xml_content) or die("cannot save XML");
Please note that saveXML(node) has a different output from saveXML().
First question:
here is my post where all usable threads with answers are listed: How do you exclude the XML prolog from output?
Second question:
I don't know of any PHP function that outputs text nodes like that.
You could:
read xml using DomDocument and save each node as string
iterate trough nodes
detect text nodes and add new lines to xml string manually
At the end you would have the same XML with text node values in new line:
<node>
some text data
</node>

XML search for a node from an array and add new data to a new node

From this tutorial - section on Adding nodes over at PHPFreaks, I did post over there but have had no replies strange as the tutorial was written by them.
http://www.phpfreaks...ndling-xml-data
When I use my xml file it creates the node but does not insert the new data. There are no page errors. I'm sure I have just missed something very simple and after hours of trying I will now bow down and ask for help.
This is the script I'm using
<?php
// isbn => pages
$page_numbers = array(
'1234' => '654', // insert data into catparent node
'5678' => '789', // insert data into catparent node
);
$dom = new DOMDocument();
$dom->load('edtest.xml');
$xpath = new DOMXPath($dom);
$items = $xpath->query('item');
foreach($items as $item)
{
$item->appendChild($dom->createElement('catparent', $page_numbers[$item->getAttribute('catcode')]));
}
$dom->save('edtest_new.xml');
?>
My XML
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<catcode>1234</catcode>
<catdesc>Systems - System Bundles</catdesc>
<price_cost>999.95</price_cost>
<price_sell>999.95</price_sell>
</item>
</items>
Output XML
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<catcode>1234</catcode>
<catdesc>Systems - System Bundles</catdesc>
<price_cost>999.95</price_cost>
<price_sell>999.95</price_sell>
<catparent></catparent> // it creates but does not insert required data
</item>
</items>
It runs the script creates the required node, but it does not insert the required data. The object of the script is to find <catcode> = 1234 and add a new <catparent> </catparent> with required data from the array.
If there is a better way to achieve the result or just the correction needed.
Thanks
You're using $item->getAttribute('catcode'). However, catcode is not an attribute of <item>, it's a child element.
Try
$catcode = $item->getElementsByTagName('catcode');
$parent_code = $page_numbers[ $catcode[0] ];
$item->appendChild( $dom->createElement('catparent', $parent_code) );

Manipulate xml file with php and dom

I have XML file as below:
<?xml version="1.0" encoding="UTF-8"?>
<root version="8.0.0.0">
<songs name="Album">
<row>
<song artist="artist_name">Track1</song>
</row>
<row>
<song artist="artist_name">Track2</song>
</row>
<row>
<song artist="artist_name">Track3</song>
</row>
<row>
<song artist="artist_name">Track4</song>
</row>
</songs>
</root>
Now i want to update this file with some more rows. How i can append data on top of the existing row elements? Also while adding new elements i want to check the tracks like - Track1, Track2 are not duplicates.
Currently i'm manipulating this xml file with php:dom, but its appending data at the bottom of the existing rows.
PHP code used to do above things is
<?php
//Creates XML string and XML document using the DOM
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->Load('C:/wamp/www/xml/test1.xml');
$root = $dom->firstChild;
$list = $root->childNodes->item(1);
if(isset($_POST['submit'])){
$artistName = $_POST['name'];
$track = $_POST['track'];
$row = $dom->createElement('row');
$list->appendChild($row);
$song = $dom->createElement('song');
$row->appendChild($song);
$song->setAttribute('artist', $artistName);
$wcm_node = $dom->createTextNode($track);
$song->appendChild($wcm_node);
}
// Code to format XML after appending data
$outXML = $dom->saveXML(); // put string in outXML
//now create a brand new XML document
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true; //yup, going to try to make this format again
//pass the output of the first bunch of stuff we did to the new XML document:
$xml->loadXML($outXML);
//now display the cleanly formatted output without using LIBXML_NOBLANKS (which may have undesired consequences
$xml->save('test1.xml'); // save as file
}
?>
Please let me know, how i can do it.
Thanks
That's not appending but prepending. DOM has a method for that, too:
DOMNode::insertBefore — Adds a new child before a reference node
Example (demo):
$dom = new DOMDocument;
$dom->loadXml('<rows><row xml:id="r1"/></rows>');
$dom->documentElement->insertBefore(
$dom->createElement('row', 'new row'),
$dom->getElementById('r1')
);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
Output:
<?xml version="1.0"?>
<rows>
<row>new row</row>
<row xml:id="r1"/>
</rows>

Categories