I am new to PHP so maybe this is silly question.
How can I easily manipulate with xml document using PHP and get data from it?
Can someone show me some example how it is done?
I tried this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
And this is my php:
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
This works for only this document, but when i try some bigger xml it doesn't work. It only writes back one tag all the time.
Your problem is that with your code you only fetch first nodes of xml documents. Try like this:
$xml = simplexml_load_file($url) ;
echo '<br /><b>all of the data:</b>'.'<br> </br><div class="data">';
echo $xml->getName() . "<br />";
foreach($xml -> children() as $name => $child ){
echo $name.'<br />';
foreach($child -> children() as $name1 => $child1 ) {
echo $name1.'<br />'; //fetching children nodes
foreach($child1 -> children() as $name2 => $child2 ) {
echo $name2.'<br />';
}
// ..... and so on...depends how deep is you node tree
}
}
you can use xpath to fetch what you want. It's easy and you can build queries with it. Try like this:
$result=$xml->xpath($your_query);
foreach($result as $bla) {
echo "$bla<br />";
}
You can use this CD catalog to test you code: http://www.w3schools.com/xml/cd_catalog.xml
Here are some examples of using xpath: http://www.w3schools.com/xpath/xpath_examples.asp
Related
In this below code, it will print all of the item of a xml file or string. But what if, i want not to print specific item? Suppose, i don't want to print the first item,then what can i do?
<?php
$note = <<<XML
<?xml version='1.0' ?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
Just skip some tag using continue statement:
$skipTag = 'from';
foreach($xml->children() as $child) {
if ($child->getName() == $skipTag) continue;
echo $child->getName() . ": " . $child . "\n";
}
I am trying to read multiple xml file from a folder and want to get tag attribute for further processing.
So far i have tried many available examples but no luck so far.
below is my code, any help will be appreciated..! Thanks..!
foreach (glob("unzip_temp/other/newfolder/*.xml") as $filename) {
$xml_file = file_get_contents($filename, FILE_TEXT);
$xml = simplexml_load_file($xml_file);
foreach ($xml->tag1 as $fileinfo) {
$name = $fileinfo['Name'];
$language = $fileinfo['Language'];
$creator = $fileinfo['Creator'];
echo "<li> $name ,$language ,$creator";
}
below is the part of xml file that i want to read:
<?xml version="1.0" encoding="utf-16"?>
<tag1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="Bethanien_Befunder_CTKolon" Language="other" Type="Simple" Creator="ADMINISTRATOR" IsMultiSelect="false" IsFloatingPickList="false" IsPhraseGenerated="false">
I got the solution: Thanks anyways
$doc = new DOMDocument();
foreach (glob("unzip_temp/other/newfolder/*.xml") as $filename) {
$doc->load($filename);
$intros = $doc->getElementsByTagName("tag1");
foreach ($intros as $intro) {
echo "<br />";
$name = $intro->getAttribute('Name');
echo $name;
echo "<br />";
echo $intro->getAttribute('Creator');
echo "<br />";
echo $intro->getAttribute('Language');
echo "<hr />";
}
}
I have the following xml document:
<?xml version="1.0"?>
<root>
<offers count="3009">
<offer>
<offerId>25</offerId>
<offer_type>for sale</offer_type>
<offer_status>available</offer_status>
<building_floors></building_floors>
<title>title</title>
<photos count="4">
<photo zIndex="1">1.jpg</photo>
<photo zIndex="2">2.jpg</photo>
<photo zIndex="3">3.jpg</photo>
<photo zIndex="4">4.jpg</photo>
</photos>
</offer>
</offers>
</root>
I am using this php code to get results from the xml:
$xml = simplexml_load_file("offers.xml")
or die("Error: Cannot create object");
foreach($xml->children() as $offers){
foreach($offers->children() as $offer => $data){
echo $data->offerId;echo "<br />";//and so on for each element
foreach ($xml->offers->offer->photos->photo as $aaa){
$photos=$aaa; echo "<br />";
//echo $xml;
$photo = explode("<br /> ", $photos);
foreach($photo as $value) echo $value; echo '<br />';//echo $value
}
}
}
When I try to loop the photo element I get all photos but only for the first offer.
My question is what I am doing wrong and why I do not get photos for each offer?
The problem is with the way you retrieve the photo elements. You start from the root element so it always give you the same node.
You should use $data like this :
foreach ($data->photos->photo as $aaa){
cleaning a bit your code you should use this to display the photo elements:
$xml = simplexml_load_file("offers.xml") or die("Error: Cannot create object");
foreach($xml->children() as $offers){
foreach($offers->children() as $offer){
echo "Offer id : " . $offer->offerId . "<br>";
foreach ($offer->photos->photo as $photo){
$val = (string) $photo;
echo $val . "<br>";
}
}
}
I've this code
$xml = simplexml_load_file("http://api.hostip.info/?ip=12.215.42.19");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child) {
echo $child->getName() . ": " . $child . "<br />";
}
If I visit http://api.hostip.info/?ip=12.215.42.19 directly on the browser, I can see the XML return but when I tried the above code, only HostipLookupResultSet<br /> gets echoed.
How do I retrieve the data from this xml? I'm interested in getting coutry and country abbreviation.
I was trying something like echo $xml->HostipLookupResultSet->gml:featureMember->Hostip->countryName but it seems it is wrong.
This might work if you query using xpath :-
// optional for register namespace into xpath
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
$result = $xml->xpath('//*[self::countryName or self::countryAbbrev]');
You need to add the namespace.
See the code bellow
/* #var $xml SimpleXMLElement */
echo $xml->getName() . "\n";
$namespaces = $xml->getDocNamespaces();
foreach($xml->children($namespaces['gml']) as $child) {
echo $child->getName() . ": " . $child . "\n";
}
You can try the following code for getting Country and Country Abbrevation:
$xml = simplexml_load_file("http://api.hostip.info/?ip=12.215.42.19");
$cntry= $xml->xpath('//gml:featureMember');
foreach($cntry as $child) {
echo $child->Hostip->countryName;
echo "<br />";
echo $child->Hostip->countryAbbrev;
}
I have xml table:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<user>
<id>1</id>
<info>
<more>
<nick>John</nick>
<adress>blabla</adress>
</more>
<more>
<nick>Mike</nick>
<adress>blablabla</adress>
<tel>blablabla</tel>
</more>
</info>
</user>
<user>
<id>2</id>
<info>
<more>
<nick>Fake</nick>
<adress>blablabla</adress>
<tel>blablabla</tel>
</more>
</info>
</user>
</table>
And i need to read data from it. I made such parser.
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br /><br />";
echo '<hr />';
foreach ($xml->children() as $child1){
//echo $child1->getName() . ": " . $child1 . "<br />"; //el
foreach($child1->children() as $child2){
if ((string)$child2 == '2') {
echo "<strong>" .$child2 . "</strong><br />";
foreach($child2->children() as $child3){
echo "<strong>".$child3->getName()."</strong>:" . $child3 . "<br />";
foreach($child3->children() as $child4){
echo $child4->getName() . ": " . $child4 . "<br />";
}
}
}
}
echo '<hr />';
echo '<br />';
}
?>
I want to make search in xml file, for example get all information about user with id 2, i try to realize it with if function but i get only id.
What is wrong?. Any suggestions??
You might want to look into using XPath, as I can't parse this martrix. XSLT is probably the better way to handle this, from what it looks like you're doing.