XML DOM Document

From Code Trash
Jump to: navigation, search

My Example

<?
$cont = file_get_contents("17242342.rss");
//echo $xml;
$xml = new DOMDocument();
$xml->loadXML($cont);
 
 
$x = $xml->getElementsByTagName('item');
//echo $x->length;
 
foreach($x as $now)
{
    $title = $now->getElementsByTagName('title')->item(0)->nodeValue;
    $dati = $now->getElementsByTagName('pubDate')->item(0)->nodeValue;
    $link = $now->getElementsByTagName('link')->item(0)->nodeValue;   
    $title = substr($title,0, strpos($title,'http://',0));
    $dati = substr($dati,0,26);
?>
    <?=$title?>
 
    <?=$dati?>
 
    <?=$link?>
<?
}
 
?>

PHP Example

<?php
 
$doc = new DOMDocument();
$doc->load( 'Users.xml' );
 
$dataset = $doc->getElementsByTagName( "dataUser" );
foreach( $dataset as $row )
{
    $xmlUserNames = $row->getElementsByTagName( "UserName" );
    $xmlUserName = $xmlUserNames->item(0)->nodeValue;
 
    $xmlEmails = $row->getElementsByTagName( "Email" );
    $xmlEmail = $xmlEmails->item(0)->nodeValue;
 
    $xmlDisplayNames = $row->getElementsByTagName( "DisplayName" );
    $xmlDisplayName = $xmlDisplayNames->item(0)->nodeValue;
 
    echo "$xmlUserName - $xmlEmail - $xmlDisplayNamen";
}
?>
 
Contents in Users.xml
 
<NewDataSet>
    <dataUser>
        <UserName>StudioAMK</UserName>
        <Email>user1@mail.com</Email>
        <DisplayName>StudioAMK.com</DisplayName>
    </dataUser>
    <dataUser>
        <UserName>User2</UserName>
        <Email>user2@mail.com</Email>
        <DisplayName>UserTwo</DisplayName>
    </dataUser>
</NewDataSet>

Search Result

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head>

<body>

  1. <a href="http://php.net/manual/en/book.dom.php" target="_blank">PHP: DOM - Manual</a>

    <?php class extDOMDocument extends DOMDocument { public function createElement($name, $value=null) ...... function.dom-domdocument-getelementsbytagname.php ...
    php.net/manual/en/book.dom.php - <a href="http://74.125.153.132/search?q=cache:9MkRQeWWdPUJ:php.net/manual/en/book.dom.php+php+DOMDocument&cd=1&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:php.net/manual/en/book.dom.php+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CAgQHzAA" target="_blank">Similar</a>
  2. <a href="http://php.net/manual/en/class.domdocument.php" target="_blank">PHP: DOMDocument - Manual</a>

    Child class of DOMDocument which has a toArray() method. Enjoy and/or improve <?php class MyDOMDocument extends DOMDocument ...
               php.net/manual/en/class.domdocument.php - <a href="http://74.125.153.132/search?q=cache:iL4Uyabe54gJ:php.net/manual/en/class.domdocument.php+php+DOMDocument&cd=2&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a>
    
  3. <a href="http://www.w3schools.com/php/php_xml_dom.asp" target="_blank">PHP XML DOM</a>

    <?php $xmlDoc = new DOMDocument(); $xmlDoc->load("note.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . ...
    <a href="http://www.w3schools.com/" target="_blank">www.w3schools.com/</a>php/php_xml_dom.asp - <a href="http://74.125.153.132/search?q=cache:COIl4XUm_UsJ:www.w3schools.com/php/php_xml_dom.asp+php+DOMDocument&cd=3&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:www.w3schools.com/php/php_xml_dom.asp+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CA8QHzAC" target="_blank">Similar</a>
  4. <a href="http://www.phpbuilder.com/manual/en/function.dom-domdocument-save.php" target="_blank">PHP: DOMDocument->save() - Manual</a>

    29 Oct 2005 ... <?php $doc = new DOMDocument('1.0'); // we want a nice output $doc->formatOutput = true; $root = $doc->createElement('book'); ...
    www.php<a href="http://builder.com/.../function.dom-" target="_blank">builder.com/.../function.dom-</a>domdocument-save.php - <a href="http://74.125.153.132/search?q=cache:e7VAEF7FVrAJ:www.phpbuilder.com/manual/en/function.dom-domdocument-save.php+php+DOMDocument&cd=4&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:www.phpbuilder.com/manual/en/function.dom-domdocument-save.php+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CBIQHzAD" target="_blank">Similar</a>
  5. <a href="http://php4every1.com/tutorials/php-domdocument-tutorial/" target="_blank">PHP DomDocument Tutorial | PHP for everyone</a>

    This will be a quick tutorial that will show you how to use PHP's DOMDocument to parse your XML so you do not have to use XML parser.
    php<a href="http://4every1.com/tutorials/" target="_blank">4every1.com/tutorials/</a>php-domdocument-tutorial/ - <a href="http://74.125.153.132/search?q=cache:EWyHTgGbxpEJ:php4every1.com/tutorials/php-domdocument-tutorial/+php+DOMDocument&cd=5&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a>
  6. <a href="http://www.ultramegatech.com/blog/2009/07/modifying-templates-using-domdocument-in-php/" target="_blank">Modifying Templates Using DOMDocument In PHP | UltraMega Blog</a>

    In the previous post, Generating (X)HTML Documents Using DOMDocument In PHP, we explored the PHP DOMDocument class by generating an (X)HTML page completely.
    <a href="http://www.ultramegatech.com/.../modifying-templates-using-" target="_blank">www.ultramegatech.com/.../modifying-templates-using-</a>domdocument-in-php/ - <a href="http://74.125.153.132/search?q=cache:2yVlG_5VQHYJ:www.ultramegatech.com/blog/2009/07/modifying-templates-using-domdocument-in-php/+php+DOMDocument&cd=6&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:www.ultramegatech.com/blog/2009/07/modifying-templates-using-domdocument-in-php/+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CBcQHzAF" target="_blank">Similar</a>
  7. <a href="http://stackoverflow.com/questions/85520/php-domdocument-stripping-html-tags" target="_blank">PHP DOMDocument stripping HTML tags - Stack Overflow</a>

    I'm working on a small templating engine, and I'm using DOMDocument to parse ... Nothing: nodeValue is the concatenation of the value portion of the tree, ...
    <a href="http://stackoverflow.com/.../" target="_blank">stackoverflow.com/.../</a>php-domdocument-stripping-html-tags - <a href="http://74.125.153.132/search?q=cache:lglmAooj8EIJ:stackoverflow.com/questions/85520/php-domdocument-stripping-html-tags+php+DOMDocument&cd=7&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:stackoverflow.com/questions/85520/php-domdocument-stripping-html-tags+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CBoQHzAG" target="_blank">Similar</a>
  8. <a href="http://www.itsalif.info/content/php-5-domdocument-creating-basic-xml" target="_blank">PHP-5 DomDocument: Creating a Basic XML | Abdullah Rubiyath ...</a>

    Since I wrote my ZCE Exam in last Sept 08', I have been using DomDocument mainly to generate XML files. Its a powerful library of PHP-5, one thing I find ...
    <a href="http://www.itsalif.info/.../" target="_blank">www.itsalif.info/.../</a>php-5-domdocument-creating-basic-xml - <a href="http://74.125.153.132/search?q=cache:0ttwBqXseFQJ:www.itsalif.info/content/php-5-domdocument-creating-basic-xml+php+DOMDocument&cd=8&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:www.itsalif.info/content/php-5-domdocument-creating-basic-xml+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CB0QHzAH" target="_blank">Similar</a>
  9. <a href="http://forums.tutorialized.com/php-91/php-domdocument-tutorial-197421.html" target="_blank">PHP DomDocument Tutorial - Tutorialized</a>

    1 post - 1 author - Last post: 2 Dec 2009
             PHP DomDocument Tutorial- PHP. Visit Tutorialized to  discuss PHP DomDocument Tutorial.
    <a href="http://forums.tutorialized.com/" target="_blank">forums.tutorialized.com/</a>php.../php-domdocument-tutorial-197421.html - <a href="http://74.125.153.132/search?q=cache:KzHLy3mXMUIJ:forums.tutorialized.com/php-91/php-domdocument-tutorial-197421.html+php+DOMDocument&cd=9&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a>
  10. <a href="http://www.zedwood.com/article/129/xml-parsing-with-php-domdocument" target="_blank">XML Parsing with PHP DOMDocument :: zedwood.com</a>

    XML Parsing with PHP DOMDocument. PHP5 has several XML parsers built in, here is some sample code for parsing with DOM XML. It is by no means an exhaustive ...
    <a href="http://www.zedwood.com/.../xml-parsing-with-" target="_blank">www.zedwood.com/.../xml-parsing-with-</a>php-domdocument - <a href="http://74.125.153.132/search?q=cache:C3XBKbI0nKMJ:www.zedwood.com/article/129/xml-parsing-with-php-domdocument+php+DOMDocument&cd=10&hl=en&ct=clnk&gl=in&client=firefox-a" target="_blank">Cached</a> - <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=related:www.zedwood.com/article/129/xml-parsing-with-php-domdocument+php+DOMDocument&sa=X&ei=-M9CS4WtBovW7AOdp9iIBA&ved=0CCMQHzAJ" target="_blank">Similar</a>
<tbody> </tbody> <tbody> </tbody>
Searches related to
php DOMDocument
<a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+domdocument+getelementsbytagname&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=1&ved=0CCYQ1QIoAA" target="_blank">php domdocument getelementsbytagname</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+domdocument+load&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=2&ved=0CCcQ1QIoAQ" target="_blank">php domdocument load</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=domdocument+php+functions&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=3&ved=0CCgQ1QIoAg" target="_blank">domdocument php functions</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+xml&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=4&ved=0CCkQ1QIoAw" target="_blank">php xml</a>
<a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+domdocument+getelementbyid&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=5&ved=0CCoQ1QIoBA" target="_blank">php domdocument getelementbyid</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+domdocument+class&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=6&ved=0CCsQ1QIoBQ" target="_blank">php domdocument class</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=domdocument+save&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=7&ved=0CCwQ1QIoBg" target="_blank">domdocument save</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=loadxml&revid=585103750&ei=-M9CS4WtBovW7AOdp9iIBA&sa=X&oi=revisions_inline&resnum=0&ct=broad-revision&cd=8&ved=0CC0Q1QIoBw" target="_blank">loadxml</a>


<tbody> </tbody>
1 <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=10&sa=N" target="_blank">2</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=20&sa=N" target="_blank">3</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=30&sa=N" target="_blank">4</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=40&sa=N" target="_blank">5</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=50&sa=N" target="_blank">6</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=60&sa=N" target="_blank">7</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=70&sa=N" target="_blank">8</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=80&sa=N" target="_blank">9</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=90&sa=N" target="_blank">10</a> <a href="http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=bc2&q=php+DOMDocument&start=10&sa=N" target="_blank">Next</a>

</body> </html>

Reference

http://www.php.net/manual/en/domdocument.getelementsbytagname.php