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>";
Related
This is my php code
$xmldata = simplexml_load_string($ops_response);
foreach($xmldata->world-patent-data->biblio-search->search-result->exchange-documents->exchange-document->bibliographic-data->parties as $item)
{
echo "<p>Applicant Name: " . $item->applicants->applicant->applicant-name->name . "</p>";
echo "<p>Doc Number: " . $item->applicants->applicant->applicant-name->doc-number . "</p>";
echo "<p>Description: " . $item->applicants->applicant->applicant-name->abstract . "</p>";
}
This is my XML File:
https://ipappatent.com/xml/document.xml
Expected Result
<p>Applicant Name: PHYLION BATTERY CO LTD</p>
<p>Doc Number: 2018101613</p>
<p>Description: A frame tube having a battery enclosure structure for an electric bike. The frame tube comprises a main body</p>
<p>Year: ASTRO ENGINEERING CO LTD [TW]</p>
<p>Category: 20180821</p>
<p>Country: A drive assemblage is described for a vehicle drivable by muscle energy and/or—in particular additionally—by motor energy</p>
Iam new to PHP and i am not very well sure on how to handle the xml output? If anyone can help me on this pls do that. Appreciate your help.
libxml_use_internal_errors(true);
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<document>
<user>John Doe</wronguser>
<email>john#example.com</wrongemail>
</document>";
$xml = simplexml_load_string($myXMLData);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($xml);
}
?>
hi try like this
Do like this. You have passes object instead of array.
Json decode and encode simplexml_load_string($ops_response).
$string = //"your xml string here";
echo "<pre>";
$json = json_decode(json_encode((array) simplexml_load_string($string)), 1);
print_r($json);
$json = $json['biblio-search']['search-result']['exchange-documents'];
foreach($json as $item)
{
echo "<p>Applicant Name: " . $item['exchange-document']['bibliographic-data']['parties']['applicants']['applicant'][0]['applicant-name']['name'] . "</p>";
echo "<p>Description: " . $item['exchange-document']['abstract']['#attributes']['lang'] . "</p>";
}
I am trying to pass a PHP variable from a link created in a for each loop.
Here is the code on the page that generates the link and variable to send:
$xmlDoc = simplexml_load_file("products.xml");
$storeArray = array();
foreach($xmlDoc->product as $Product) {
echo "Name: " . $Product->name . ", ";
echo "Price: " . $Product->price . ", ";
$store = (string)$Product->store;
if (!array_key_exists($store, $storeArray)) {
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
$store . "</a>";
}}
foreach ($storeArray as $store) {
echo $store . "<br>";
}
Here is the code on the page that receives the variable:
$searchByStore = $_GET["storeSearch"];
echo "Store search variable is: " . $searchByStore;
The searchByStore variable is not being echoed. Any advice?
Here is how this url appears in the browser:
.../searchResults.php?storeSearch=%3C?php%20echo%20Best%20Buy;%20?%3E
Instead of the normal way, which is:
.../searchResults.php?storeSearch=Best%20Buy
And here is the XML file:
<product type="Electronics">
<name> Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
</product>
<product type="Electronics">
<name>Lap top</name>
<price>599.99</price>
<store>Best Buy</store>
</product>
<product type="Hardware">
<name>Hand Saw</name>
<price>99.99</price>
<store>Lowes</store>
</product>
</products>
this is wrong:
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
$store . "</a>
";
change it to
$storeArray[$store] = "<a href='searchResults.php?storeSearch=".$store."'>".$store."</a>";
You cannot embed PHP code inside PHP code:
$storeArray[$store] = "<a href='searchResults.php?storeSearch=<?php echo $store; ?>'>" .
^^^^^^^^^^^^^^^^^^^^^
It should be
$storeArray[$store] = "<a blah blah storeSearch=" . $store . ">";
or
$storeArray[$store] = "<a blah blah storeSearch={$store}>";
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 />"
}
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
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.