Read XML PHP DOM with XPATH - php

I have a question.
I have the xml
http://maps.googleapis.com/maps/api/geocode/xml?address=new+york&sensor=true
I want to read from example
GeocodeResponse/result/geometry/location/lat
and
GeocodeResponse/result/geometry/location/lng
maybe using XPATH and this is what i have so far ...
<?php
$Address = "new+york";
$Query = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
$XmlResponse = file_get_contents($Query);
$doc = new DOMDocument();
$doc->loadXML($XmlResponse);
$root = $doc->getElementsByTagName( "GeocodeResponse" );
foreach( $root as $val )
{
$hrefs = $val->getElementsByTagName( "status" );
$status = $hrefs->item(0)->nodeValue;
foreach( $hrefs as $val2 )
{
$hrefs2 = $val2->getElementsByTagName( "type" );
$type = $hrefs2->item(0)->nodeValue;
echo "Type is: $type <br>";
}
echo "Status is: $status <br>";
}
?>
Can i have some advices?
maybe i can use
$xpath = new DOMXPath($xml);
$hrefs = $xpath->evaluate("/page");
UPDATE!!
I have managed to get the result by this ...
$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');
$root = $doc->getElementsByTagName( "location" );
foreach( $root as $val )
{
$hrefs = $val->getElementsByTagName( "lat" );
$status = $hrefs->item(0)->nodeValue;
echo "Status is: $status <br>";
}
but i want something without foreach like
$xpath = new DOMXPath($doc);
$res = $xpath->evaluate('//GeocodeResponse/result/geometry');
$hrefs = $val->getElementsByTagName( "lat" );
$status = $hrefs->item(0)->nodeValue;
echo "Status is: $status <br>";
Is this possible?

You may want to switch to JSON that is much easier to parse:
http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=true
$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=true');
$geocodeResponse = json_decode($json, true);
foreach($geocodeResponse['results'] as $result){
echo $result['geometry']['location']['lat'].', '.$result['geometry']['location']['lng'] .'<br>';
}

Related

How to create looped XML file from HTML in PHP?

I would like to be able to create an XML file from some of the content of a html page. I have tried intensively but seem to miss something.
I have created two arrays, I have setup a DOMdocument and I have prepared to save an XML file on the server... I have tried to make tons of different foreach loops all over the place - but it won't work.
Here is my code:
<?php
$page = file_get_contents('http://www.halfmen.dk/!hmhb8/score.php');
$doc = new DOMDocument();
$doc->loadHTML($page);
$score = $doc->getElementsByTagName('div');
$keyarray = array();
$teamarray = array();
foreach ($score as $value) {
if ($value->getAttribute('class') == 'xml') {
$keyarray[] = $value->firstChild->nodeValue;
$teamarray[] = $value->firstChild->nextSibling->nodeValue;
}
}
print_r($keyarray);
print_r($teamarray);
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$droot = $doc->createElement('ROOT');
$droot = $doc->appendChild($droot);
$dsection = $doc->createElement('SECTION');
$dsection = $droot->appendChild($dsection);
$dkey = $doc->createElement('KEY');
$dkey = $dsection->appendChild($dkey);
$dteam = $doc->createElement('TEAM');
$dteam = $dsection->appendChild($dteam);
$dkeytext = $doc->createTextNode($keyarray);
$dkeytext = $dkey->appendChild($dkeytext);
$dteamtext = $doc->createTextNode($teamarray);
$dteamtext = $dteam->appendChild($dteamtext);
echo $doc->save('xml/test.xml');
?>
I really like simplicity, thank you.
You need to add each item in one at a time rather than as an array, which is why I build the XML for each div tag rather than as a second pass. I've had to assume that your XML is structured the way I've done it, but this may help you.
$page = file_get_contents('http://www.halfmen.dk/!hmhb8/score.php');
$doc = new DOMDocument();
$doc->loadHTML($page);
$score = $doc->getElementsByTagName('div');
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$droot = $doc->createElement('ROOT');
$droot = $doc->appendChild($droot);
foreach ($score as $value) {
if ($value->getAttribute('class') == 'xml') {
$dsection = $doc->createElement('SECTION');
$dsection = $droot->appendChild($dsection);
$dkey = $doc->createElement('KEY', $value->firstChild->nodeValue);
$dkey = $dsection->appendChild($dkey);
$dteam = $doc->createElement('TEAM', $value->firstChild->nextSibling->nodeValue);
$dteam = $dsection->appendChild($dteam);
}
}

DOM Xpath get desired result only

$some_link = 'http://www.example.com';
$abc = 'killer';
$bcd = 'awsome';
$cde = 'qwerty';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
#$dom->loadHTMLFile($some_link);
$html = getTags( $dom, $abc, $bcd, $cde );
echo $html;
function getTags( $dom, $abc, $bcd, $cde ){
$html = '';
$domxpath = new DOMXPath($dom);
$newDom = new DOMDocument;
$newDom->formatOutput = true;
$defffff = $domxpath->query("//$abc" . '[#' . $bcd . "='$cde']");
// since above returns DomNodeList Object
// converting to string(html)
$i = 0;
while( $myItem = $defffff->item($i++) ){
$node = $newDom->importNode( $myItem, true ); // import node
$newDom->appendChild($node); // append node
}
$html = $newDom->saveHTML();
return $html;
}
?>
this is the whole code. it is returning multiple results in a row, now what I want is to have ONLY the result no.1 and no.5. How can I do it?
I am new to DOM, tried several things but no success. Thanks in Advance
Change this
$i = 0;
while( $myItem = $defffff->item($i++) ){
$node = $newDom->importNode( $myItem, true ); // import node
$newDom->appendChild($node); // append node
}
into this, in order to append only selected nodes
$i = 0;
while( $myItem = $defffff->item($i++) ){
if ($i==0 or $i==4){
$node = $newDom->importNode( $myItem, true ); // import node
$newDom->appendChild($node); // append node
}
}
or you if you know the indexes you want already, you can do this
$myIndexes = array (0,4);
foreach ($myIndexes as $i){
$myItem = $defffff->item($i++);
$node = $newDom->importNode( $myItem, true ); // import node
$newDom->appendChild($node); // append node
}

Xpath for extracting links

I create an scraper for an automoto site and first I want to get all manufactures and after that all links of models for each manufactures but with the code below I get only the first model on the list. Why?
<?php
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.auto-types.com');
$xpath = new DOMXPath($dom);
$entries = $xpath->query("//li[#class='clearfix_center']/a/#href");
$output = array();
foreach($entries as $e) {
$dom2 = new DOMDocument();
#$dom2->loadHTMLFile('http://www.auto-types.com' . $e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$data['newLinks'] = trim($xpath2->query("//div[#class='modelImage']/a/#href")->item(0)->textContent);
$output[] = $data;
}
echo '<pre>' . print_r($output, true) . '</pre>';
?>
SO I need to get: mercedes/100, mercedes/200, mercedes/300 but now with my script i get only the first link so mercedes/100...
please help
You need to iterate through the results instead of just taking the first item:
$items = $xpath2->query("//div[#class='modelImage']/a/#href");
$links = array();
foreach($items as $item) {
$links[] = $item->textContent;
}
$data['newLinks'] = implode(', ', $links);

Getting XML attributes in PHP

Looked at a few other SO posts on this but no joy.
I've got this code:
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);
foreach ($xml->entry as $val) {
echo "RESULTS: " . $val->attributes() . "\n";
but I can't get any results.
I'm specifically interested in getting the ID value which would be 549592189 in this fragment:
<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&uo=2</id>
Any suggestions?
SimpleXML gives you can easy way to drill down in the XML structure and get the element(s) you want. No need for the regex, whatever it does.
<?php
// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);
// Get the entries
$entries = $xml->entry;
foreach($entries as $e){
// Get each entriy's id
$id = $e->id;
// Get the attributes
// ID is in the "im" namespace
$attr = $id->attributes('im', TRUE);
// echo id
echo $attr['id'].'<br/>';
}
DEMO: http://codepad.viper-7.com/qNo7gs
Try with xpath:
$doc = new DOMDocument;
#$doc->loadHTML($string);
$xpath = new DOMXpath($doc);
$r = $xpath->query("//id/#im:id");
$id = $r->item(0)->value;
Try:
$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}

Getting element names/values with XML/Xpath/PHP

I have an XML schema that looks as follows:
<xml>
<user id="1">
<first_name>Bill</first_name>
<last_name>Steve</last_name>
<phone_numbers>
<work>xxx-xxx-xxxx</work>
<home>xxx-xxx-xxxx</home>
</phone_numbers>
</user>
<user id="2">
........
</user>
</xml>
Im working on parsing all of this information into PHP using DOM. Ex.
$userInfo = $doc->getElementsByTagName( "user" );
foreach($userInfo as $row)
{
$first_name = $row->getElementsByTagName("first_name");
}
When I try to nest this to select the phone numbers however I get an error. I've tried using XPath to select the phone numbers with equal problems. I tried something along the lines of
$userInfo = $doc->getElementsByTagName( "user" );
foreach($userInfo as $row)
{
$phoneInfo = $row->getElementsByTagName("phone_numbers");
foreach($phoneInfo as $row2)
{
$work = $row2->getElementsByTagName("work");
}
}
Im curious if Im doing something fundamentally wrong, or how to get this going. I've been tearing my hair out for a few hours now.
You can't get the value directly from a DOMNodeList Object, try this :
$userInfo = $doc->getElementsByTagName( "user" );
foreach($userInfo as $row)
{
$phoneInfo = $row->getElementsByTagName("phone_numbers");
foreach($phoneInfo as $row2)
{
// get the value from the first child
$work = $row2->getElementsByTagName("work")->item(0)->nodeValue;
$home = $row2->getElementsByTagName("home")->item(0)->nodeValue;
}
}
Well, you could switch it to SimpleXml which makes this type of parsing easier:
$userInfo = $doc->getElementsByTagName( "user" );
foreach ($userInfo as $user) {
$node = simplexml_import_dom($user);
$id = (string) $node['id'];
$first = (string) $node->first_name;
$last = (string) $node->last_name;
$workPhone = (string) $node->phone_numbers->work;
$homePhone = (string) $node->phone_numbers->home;
}
Now, in DomDocument, you could do this by using DomXpath:
$userInfo = $doc->getElementsByTagName( "user" );
$xpath = new DomXpath($doc);
foreach ($userInfo as $user) {
$id = $user->getAttribute('id');
$first = $xpath->query('//first_name', $user)->item(0)->textContent;
$last = $xpath->query('//last_name', $user)->item(0)->textContent;
$work = $xpath->query('//phone_numbers/work', $user)->item(0)->textContent;
$home = $xpath->query('//phone_numbers/home', $user)->item(0)->textContent;
}
Note that the above code (both parts) require that the format is exactly that. If you have conditionals, you might want to change it to something like this (the firstname conditional only):
$userInfo = $doc->getElementsByTagName( "user" );
$xpath = new DomXpath($doc);
foreach ($userInfo as $user) {
$id = $user->getAttribute('id');
$firstQuery = $xpath->query('//first_name', $user);
if ($firstQuery->length > 0) {
$first = $firstQuery->item(0)->textContent;
} else {
$first = '';
}
}

Categories