How to display only necessary information from xml - php

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.

Related

Trying to get the id of XML when button is clicked and list the contents on another page PHP

I'm trying to get the ID from the the hotellist.php page. I'm not sure how to do it in XML since I'm new to it. Please explain.
<?php
$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");
foreach($xml->children() as $hotels) {
echo "<tr><td width='20%'><img src='".$hotels->image."'></td>";
echo "<td width='60%'><h1>".$hotels->name."</h1><h3>";
echo $hotels->address . "</h3></td>";
echo "<td width='20%'><table id='price'><tr><td><h1>$".$hotels->price . "</h1></td></tr>";
echo "<tr><td><img src='image/btn_viewdetails.jpg'/></table></td></tr>";
}
?>
And make the full contents appear in hotel.php
Here is my XML
<?xml version="1.0" encoding="utf-8"?>
<hotellist>
<hotel>
<id>1</id>
<image>image/hotel/orchidgardenhotel.jpg</image>
<name>Rizqun Hotel</name>
<address>No. 15, Spg 447, Kg Kapok, Jalan Muara</address>
<phone>8655425</phone>
<price>60</price>
</hotel>
<hotel>
<id>2</id>
<image>image/hotel/orchidgardenhotel.jpg</image>
<name>Orchid Hotel</name>
<address>No. 15, Spg 447, Kg Kapok, Jalan Muara</address>
<phone>8655425</phone>
<price>420</price>
</hotel>
</hotellist>
Try following code:
$id = $_REQUEST['id'];
$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");
$hotels = $xml->xpath("//hotellist/hotel[id='{$id}']");
foreach($hotels as $hotel) {
echo $hotel->image . "\n";
echo $hotel->name . "\n";
echo $hotel->address . "\n";
echo "$".$hotel->price . "\n";
echo $hotel->id . "\n";
}

XML parsing: Skipping specific item or tag

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

XML feeds and namespaces with PHP, SimpleXML and Xpath

Hi I have been struggling with this puzzle for some time now how to get data out of this embedded namespaces here is the xml feed I have cut it down to make easier to read
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns="http://www.w3.org/2005/Atom" xmlns:sc="http://schemas.sage.com/sc/2009" xmlns:crm="http://schemas.sage.com/crmErp/2008" xmlns:sdatasync="http://schemas.sage.com/sdata/sync/2008/1" xmlns:sdata="http://schemas.sage.com/sdata/2008/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:sme="http://schemas.sage.com/sdata/sme/2007" xmlns:http="http://schemas.sage.com/sdata/http/2008/1">
<author />
<category term="tradingAccount" />
<generator />
<subtitle>Provides a feed containing tradingAccount details</subtitle>
<title>Sage Accounts 50 | tradingAccount - Practice Accounts </title>
<entry>
<author />
<content type="html"><![CDATA[<html>
</html>]]></content>
<id>http://computer_1:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68}/tradingAccountCustomer(58b10585-63d4-4bb8-adb3-7096d9b055d9)?format=atomentry</id>
<link href="http://computer_1:5493/sdata/accounts50/GCRM/-/tradingAccountCustomer" rel="via" type="application/atom+xml" />
<published>2015-03-13T21:28:59.000+00:00</published>
<updated>2015-07-01T21:33:13.000+01:00</updated>
<http:httpStatus>200</http:httpStatus>
<sdata:payload>
<crm:tradingAccount sdata:url="http://computer_1:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68}/tradingAccountCustomer(58b10585-63d4-4bb8-adb3-7096d9b055d9)?format=atomentry" sdata:uuid="58b10585-63d4-4bb8-adb3-7096d9b055d9">
<crm:active>true</crm:active>
<crm:customerSupplierFlag>Customer</crm:customerSupplierFlag>
<crm:companyPersonFlag>Company</crm:companyPersonFlag>
<crm:invoiceTradingAccount xsi:nil="true" />
<crm:openedDate>2013-04-22</crm:openedDate>
<crm:reference>AAA</crm:reference>
<crm:name>BBB</crm:name>
</crm:tradingAccount>
</sdata:payload>
</entry>
<opensearch:totalResults>118</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>118</opensearch:itemsPerPage>
</feed>
I am trying to access the <crm:reference><crm:name><crm:openedDate> namespace but failing I have looked at similar projects but I think its a namespace within a namespace
here is my attempt at parsing the data
<?php
$xml = simplexml_load_file("sage.xml");
$xml->registerXPathNamespace('sdata', 'http://schemas.sage.com/sdata/sync/2008/1');
foreach($xml->xpath("//sdata:payload") as $entry) {
$entry->registerXPathNamespace('sdata', 'http://schemas.sage.com/sdata/sync/2008/1');
$entry->registerXPathNamespace('crm', 'http://schemas.sage.com/crmErp/2008');
$content = $entry->xpath("/sdata:payload/crm:tradingAccount/crm:active");
//$article = feed->xpath("/sdata:payload/crm:tradingAccount/crm:active");
foreach($content as $c) {
// echo $c->reference . " | " . $c->name . "/" . $c->accountOpenedDate . "<br />\n";
}
}
var_dump($content);
var_dump($c);
any pointers to my problem would help
foreach($xml->xpath("//sdata:payload") as $entry) {
// xpath here must be from payload to tradingAccount
$content = $entry->xpath("./crm:tradingAccount");
foreach($content as $c) {
// Make set of children with prefix crm
$nodes = $c->children('crm', true);
echo $nodes->reference . " | " . $nodes->name . " / " . $nodes->openedDate . "<br />\n";
}
}
It could be as simple as:
$xml = simplexml_load_file("sage.xml");
$reference = $xml->xpath("//crm:reference");
$name = $xml->xpath("//crm:name");
$openedDate = $xml->xpath("//crm:openedDate");
echo "reference: ". $reference[0] . "<br>";
echo "name: ". $name[0] . "<br>";
echo "openedDate: ". $openedDate[0] . "<br>";
If you would like to use a foreach loop to get all children of 'crm' you can do the following. You need atleast 5.2.0 if you want to set children to prefix (true)
$xml = simplexml_load_file("sage.xml");
foreach($xml->xpath("//crm:tradingAccount") as $entry) {
$child = $entry->children('crm', true);
}
echo "reference: ". $child->reference . "<br>";
echo "name: ". $child->name . "<br>";
echo "openedDate: ". $child->openedDate . "<br>";

xml to mysql simplexml_load_string, no insert

the values are not being inserted and also no error is being displayed. Any idea as to what is happening with my insert?
<?php
$string = <<<XML
<?xml version='1.0'?>
<setnames>
<country>
<countryCode>AD</countryCode>
<countryName>Andorra</countryName>
</country>
<country>
<countryCode>AE</countryCode>
<countryName>United Arab Emirates</countryName>
<isoNumeric>784</isoNumeric>
</country>
<country>
<countryCode>AF</countryCode>
<countryName>Afghanistan</countryName>
<isoNumeric>784</isoNumeric>
</country>
</setnames>
XML;
$xml = simplexml_load_string($string);
foreach ($xml as $country)
{
mysqli INSERT INTO setnames
VALUES ($country->countryCode, $country->countryName, $country->isoNumeric);
echo $country->countryCode . "<br />";
echo $country->countryName . "<br />";
echo $country->isoNumeric . "<br />"
}
$xml variable is an object, nor an array. So, you should do your foreach like this:
foreach ($xml->country as $country)
{
mysqli INSERT INTO setnames
VALUES ($country->countryCode, $country->countryName, $country->isoNumeric);
echo $country->countryCode . "<br />";
echo $country->countryName . "<br />";
echo $country->isoNumeric . "<br />"
}

xml manipulation with php

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

Categories