Get XML values using PHP - php

I have a XML file. Here is a small version of that.
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
<title>Liste des ebooks</title>
<updated>2012-03-01T01:23:24Z</updated>
<author>
<name>Drown Del</name>
</author>
<opensearch:totalResults>2338</opensearch:totalResults>
<opensearch:itemsPerPage>100</opensearch:itemsPerPage>
<entry>
<category term="Romans" label="Romans"/>
<category term="Aventures" label="Aventures"/>
</entry>
</feed>
First I would like to know how do we call something like opensearch:totalResults in XML terms.
And I need your help with obtaining following values with PHP.
<opensearch:totalResults>2338</opensearch:totalResults> I need to get 2338 to a PHP variable.
Thank you.
Thank you all for your answers.
I could fix it with following way.
$xml = simplexml_load_string($xmltext);
$val = $xml->xpath('opensearch:totalResults');
echo $val[0];

parse all of this information into PHP using DOM. Ex.
$doc = new DOMDocument;
$doc->loadXML($xml); //$xml is your xml string
echo $doc->getElementsByTagName("totalResults")->item(0)->nodeValue;

For your first question, opensearch:totalResults is the qualified name of a start tag. It is called a qualified name (you might come across this as QName) because it contains the namespace (opensearch) for the tag.
For your second question, you can easily parse your XML into a DOMDocument and then query it for the value of the relevant tag. There are lots of examples on SO and of course on Google; a basic one from PHP.net is here.
Important note: Your current XML document does not contain an XML namespace declaration for the opensearch namespace, and will not parse as a result. You need to add such a declaration by making a modification:
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
If you need more powerful querying you can also use XPath. A minimal example would look like:
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//opensearch:totalResults');
foreach ($nodes as $node) {
echo $node->nodeValue;
}

opensearch is a namespace, so you can try to access it like:
$yourXml->children('openSearch', true)->totalResults
Hope it helps

Check this out the exact result in PHP
<?php
$xml ='<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
<title>Liste des ebooks</title>
<updated>2012-03-01T01:23:24Z</updated>
<author>
<name>Drown Del</name>
</author>
<opensearch:totalResults>2338</opensearch:totalResults>
<opensearch:itemsPerPage>100</opensearch:itemsPerPage>
<entry>
<category term="Romans" label="Romans"/>
<category term="Aventures" label="Aventures"/>
</entry>
</feed>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$xmlD = simplexml_import_dom($dom);
echo $xmlD->totalResults;
?>

Read your xml file with simplexml_load_file as an object
Then get your variable like this:
$object->{'opensearch:totalResults'};

Related

SimpleXML: How to parse nodenames including dots with xpath query

I have to parse the following XML:
<BMECAT version="2005" xmlns="http://www.bmecat.org/bmecat/2005fd">
<T_NEW_CATALOG prev_version="0">
<PRODUCT>
<NAME>Test</NAME>
<USER_DEFINED_EXTENSIONS>
<UDX.VENDOR.ATTRIBUTE lang="de">German</UDX.VENDOR.ATTRIBUTE>
<UDX.VENDOR.ATTRIBUTE lang="en">English</UDX.VENDOR.ATTRIBUTE>
</USER_DEFINED_EXTENSIONS>
</PRODUCT>
</T_NEW_CATALOG>
</BMECAT>
I need to get the value "German" via xpath. Everything I have tried so far didn't work so I need some help. I think the problem is that the tagname includes dots. I have found no way to escape the xpath expression.
The XML you show cannot be parsed because it is malformed (USER_DEFINED_EXTENSIONS is not closed properly). Assuming well-formed input, you could use DOMDocument instead of SimpleXML:
<?php
$xml = <<<'XML'
<BMECAT version="2005" xmlns="http://www.bmecat.org/bmecat/2005fd">
<T_NEW_CATALOG prev_version="0">
<PRODUCT>
<NAME>Test</NAME>
<USER_DEFINED_EXTENSIONS>
<UDX.VENDOR.ATTRIBUTE lang="de">German</UDX.VENDOR.ATTRIBUTE>
<UDX.VENDOR.ATTRIBUTE lang="en">English</UDX.VENDOR.ATTRIBUTE>
</USER_DEFINED_EXTENSIONS>
</PRODUCT>
</T_NEW_CATALOG>
</BMECAT>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("bme",
"http://www.bmecat.org/bmecat/2005fd");
$expression = 'string(
/bme:BMECAT
/bme:T_NEW_CATALOG
/bme:PRODUCT
/bme:USER_DEFINED_EXTENSIONS
/bme:UDX.VENDOR.ATTRIBUTE[#lang = "de"]
)';
var_dump($xpath->evaluate($expression));
And the result will be
German
If you need to use SimpleXML, you really need to show your current PHP code.

Adding in new XML root node

I need to add in a new root node to the following XML
<?xml version="1.0"?>
<unit>
<source>
<id>ANCH02</id>
<uri>http://www.hamiltonisland.biz/tabid/339/Default.aspx</uri>
</source>
</unit>
to become
<?xml version="1.0"?>
<units>
<unit>
<source>
<id>ANCH02</id>
<uri>http://www.hamiltonisland.biz/tabid/339/Default.aspx</uri>
</source>
</unit>
</units>
How could I do this? It doesn't seem like SimpleXMLElement has this functionality. I have also looked at this DomNode example http://php.net/manual/en/domnode.insertbefore.php but it doesnt seem to be able to add in a new root node.
This seem to work
$units = $dom->createElement('units');
$units->appendChild($dom->documentElement);
$dom->appendChild($units);
DEMO
DOMDocument:
$yourDOMDOMDocument ... <--- already loaded XML
$doc = new DOMDocument();
$doc->appendChild($doc->createElement('Units'));
$doc->documentElement->appendChild($doc->importNode($yourDOMDocument->documentElement));
Or. if you have your XML as SimpleXMLElement already:
$yourSimpleXML ... <--- already loaded XML
$doc = new DOMDocument();
$doc->appendChild($doc->createElement('Units'));
$domnode = dom_import_simplexml($yourSimpleXML);
$doc->documentElement->appendChild($doc->importNode($domnode));
//if you want it back as SXE:
$newSimpleXMLElement = simplexml_import_dom($doc);

PHP : how to parse XML with nested xpath elements

Here is the XML that I am working on :
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
<channel>
<item>
<title>A Simple Title</title>
<noo:subcategory>the sub category</noo:subcategory>
<noo:relatedInfos>
<noo:teams>
<noo:team id="3">New York</noo:team>
<noo:team id="4">Las Vegas</noo:team>
</noo:teams>
</noo:relatedInfos>
</item>
</channel>
</rss>
I am doing this php code to get the two "team" but it does not work ($xml has the previous content) :
$xml_datas = simplexml_load_string($xml);
foreach($xml_datas->channel->item as $item){
$noo = $item->children('noo');
echo $noo->team;
}
Do you have any idea why it is not working ?
Thanks
See if this helps:
<?php // RAY_temp_userco.php
error_reporting(E_ALL);
$xml = <<<ENDXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
<channel>
<item>
<title>A Simple Title</title>
<noo:subcategory>the sub category</noo:subcategory>
<noo:relatedInfos>
<noo:teams>
<noo:team id="3">New York</noo:team>
<noo:team id="4">Las Vegas</noo:team>
</noo:teams>
</noo:relatedInfos>
</item>
</channel>
</rss>
ENDXML;
$obj = simplexml_load_string($xml);
$ns = $obj->getNamespaces(TRUE);
foreach($obj->channel->item as $item){
$noo = $item->children($ns['noo']);
var_dump($noo);
}
"noo" is just a local alias for that namespace, and the ->children() method (and most XML handling functions) want to know its actual global identifier, which is the URI in the xmlns attribute.
You need to either specify the full identifier of the namespace (i.e. ->children('http://www.myscheme.com/schema')) or set the optional second parameter to tell SimpleXML to look up the prefix (->children('noo', true). The second may be more readable, but it will break if a future document has the same schema, but gives the namespace a different local alias.
Additionally, the team nodes aren't directly under the item node, so you need to traverse further to get them:
// Give the namespace a readable name that won't change
define('NS_NOO', 'http://www.myscheme.com/schema');
$xml_datas = simplexml_load_string($xml);
foreach($xml_datas->channel->item as $item){
$teams = $item->children(NS_NOO)->relatedInfo->teams;
echo $teams->team[0];
}

How to extract the content of <uri></uri> in a XML document?

I have a document that it's structure is like below.
There are a lot of <entry>. My question is how can I output the <uri> of each entry? And another question, how can I output only the USERNAME?
This is the file I want to get the usernames http://search.twitter.com/search.atom?q=yankees
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:google="http://base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/" xmlns:georss="http://www.georss.org/georss">
<entry>
<author>
<name></name>
<uri>http://twitter.com/USERNAME</uri>
</author>
</entry>
<?php
$xml = new DOMDocument;
// link to ur file
$xml->load('');
foreach ($xml->getElementsByTagName('entry') as $product )
{
$append = array();
foreach($product->getElementsByTagName('uri') as $name ) {
// Stick $name onto the array
$append[] = $name;
}
}
$result = $xml->saveXML();
print_r(str_replace('http://twitter.com/','',$result));
?>
You can use Xpath queries
http://www.php.net/manual/en/simplexmlelement.xpath.php
or
http://php.net/manual/en/domxpath.query.php
You should use SimpleXML some kind of a loop which goes trough all the s.
(foreach($xml->entry as $entry) loop should work fine, I think.)
And for the second: if it is always http://twitter.com/USERNAME, simply count the prefix's length than use a substr.
Resources to use: substr, SimpleXML, SimpleXML

How do I parse an individual entry from XML using PHP?

I am trying to parse an individual element from an XML string using PHP. The issue is that this individual element occurs before the entries start. The XML is below:
<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="shopping#products" gd:etag=""lm_25heFT8yiumci9EH1kItJBpg/Sj5O9aXZ82PKpx3N2C3uQYMhNYE"" xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:s="http://www.google.com/shopping/api/schemas/2010">
<openSearch:totalResults>64</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry >...</entry>
<entry >...</entry>
</feed>
I am trying to parse out the "64" in the opensearch:totalResults tag. How do I this and assign it to a variable in php? I tried:
$url = 'url of xml feed';
$xml = simplexml_load_file($url);
$entries =$xml->entry[0]->openSearch:totalResults;
// also tried $entries =$xml->openSearch:totalResults;
echo $entries;
but it's not working. Any advice?
You need to register namespace in order to access these nodes:
$xml = simplexml_load_file('file.xml');
$xml->registerXPathNamespace('os', 'http://a9.com/-/spec/opensearchrss/1.0/');
$nodes = $xml->xpath('os:totalResults');
$totalResults = (string)$nodes[0];
You can also use http://it1.php.net/manual/en/simplexmlelement.children.php (using the $ns parameter)
that is less resource intensive.

Categories