Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Saturday, January 7, 2012

SimpleXML, Birds Eye View

XML is always been a pain to me. The way I learn the best is by looking at examples. Below is a script from a project that I was recently working on using php SimpleXML object. It shows an organize format of the XML file to serve as a road map


<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie page="0" numpages="38">
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;



//Output
$xml = new SimpleXMLElement($xmlstr);

echo '<pre>';
print_r($xml->movie);
echo '</pre>';
echo 'Example:' . $xml->movie[0]->title;

?>


Script above will output


SimpleXMLElement Object
(
[@attributes] => Array
(
[page] => 0
[numpages] => 38
)

[title] => PHP: Behind the Parser
[characters] => SimpleXMLElement Object
(
[character] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Ms. Coder
[actor] => Onlivia Actora
)

[1] => SimpleXMLElement Object
(
[name] => Mr. Coder
[actor] => El ActÓr
)

)

)

[plot] =>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.

[great-lines] => SimpleXMLElement Object
(
[line] => PHP solves all my web problems
)

[rating] => Array
(
[0] => 7
[1] => 5
)

)
Example:PHP: Behind the Parser

Friday, September 23, 2011

DOMDocument, accessing other sites HTML. Bypassing the Same Origin Policy

Below is a code that retrieves <a> tags from a webpage outside the server using the DOMDocument. A built in PHP class that retrieves HTML and XML from a webpage. Bypassing the Same Origin Policy. This is an alternative to cURL which downloads the entire HTML page.



$keywords = array();
$domain = array('http://bing.com');//select website to extract

$doc = new DOMDocument;
$doc->preserveWhiteSpace = FALSE;

foreach ($domain as $key => $value) {
@$doc->loadHTMLFile($value); //Load HTML from a file
$anchor_tags = $doc->getElementsByTagName('a'); //get <a> tags by accessing the DOM
foreach ($anchor_tags as $tag) {
$keywords[] = strtolower($tag->nodeValue);
}
}
echo '
';
print_r ($keywords);
echo '
';