Friday, 23 December 2016

XML DOM LOOP - LOAD &OUTPUT








<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
";


************************************************

XmlDomLoop.php

***************************************************

<?php

$xmlDoc = new DOMDocument();

$xmlDoc->load("note.xml");


$x = $xmlDoc->documentElement;

foreach ($x->childNodes AS $item) {

  print $item->nodeName . " = " . $item->nodeValue . "<br>";

}

?>

*****************************************************************************

*****************************************************************************
OUTPUT
******************************************************************************

#text = 
to = Tove
#text = 
from = Jani
#text = 
heading = Reminder
#text = 
body = Don't forget me this weekend!
#text = 

*******************************************************************************  

XML DOM - LOAD AND OUTPUT






<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
";


************************************************

XmlDom.php

***************************************************

<?php

$xmlDoc = new DOMDocument();

$xmlDoc->load("note.xml");

print $xmlDoc->saveXML();

?>

*****************************************************************************
*****************************************************************************
OUTPUT
******************************************************************************

Tove Jani Reminder Don't forget me this weekend!

*******************************************************************************  

XMP - EXPAT PARSER






NOTE.XML

*****************************************************************************

<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

*****************************************************************************

XmlExpatParser.php

******************************************************************************

<?php
// Initialize the XML parser
$parser=xml_parser_create();
// Function to use at the start of an element
function start($parser,$element_name,$element_attrs) {
  switch($element_name) {
    case "NOTE":
    echo "-- Note --<br>";
    break;
    case "TO":
    echo "To: ";
    break;
    case "FROM":
    echo "From: ";
    break;
    case "HEADING":
    echo "Heading: ";
    break;
    case "BODY":
    echo "Message: ";
  }
}
// Function to use at the end of an element
function stop($parser,$element_name) {
  echo "<br>";
}
// Function to use when finding character data
function char($parser,$data) {
  echo $data;
}
// Specify element handler
xml_set_element_handler($parser,"start","stop");
// Specify data handler
xml_set_character_data_handler($parser,"char");
// Open XML file
$fp=fopen("note.xml","r");
// Read data
while ($data=fread($fp,4096)) {
  xml_parse($parser,$data,feof($fp)) or
  die (sprintf("XML Error: %s at line %d",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser)));
}
// Free the XML parser
xml_parser_free($parser);
?>

******************************************************************************

OUTPUT
*******************************************************************************
-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!
*******************************************************************************

XML - GET ATTRIBUTE VALUE







books.xml
************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
************************************************************************

XmlGetAttributeValueLoop.php
************************************************************************

<?php

$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");

foreach($xml->children() as $books) { 

    echo $books->title['lang'];

    echo "<br>"; 

}

?> 

************************************************************************
OUTPUT
*************************************************************************
en
enen-usen-us
*************************************************************************

XML - GET ATTRIBUTE VALUE




books.xml
************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
************************************************************************

XmlGetAttributeValue.php
************************************************************************

<?php

$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");

echo $xml->book[0]['category'] . "<br>";

echo $xml->book[1]->title['lang']; 

?>

************************************************************************
OUTPUT
*************************************************************************
COOKING

en
*************************************************************************

XML - GET NODE VALUE LOOP






books.xml
************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
************************************************************************

XmlGetNodevalueLoop.php
************************************************************************

<?php

$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");

foreach($xml->children() as $books) { 

    echo $books->title . ", "; 

    echo $books->author . ", "; 

    echo $books->year . ", ";

    echo $books->price . "<br>"; 

?>

************************************************************************
OUTPUT
*************************************************************************

Everyday Italian, Giada De Laurentiis, 2005, 30.00Harry Potter, J K. Rowling, 2005, 29.99XQuery Kick Start, James McGovern, 2003, 49.99Learning XML, Erik T. Ray, 2003, 39.95

*************************************************************************

XML - GET SPECIFIC ELEMENT NODE VALUE




books.xml
************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
************************************************************************

XmlGetSpecificNodevalue.php
************************************************************************
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]->title . "<br>";
echo $xml->book[1]->title;
?>
************************************************************************
OUTPUT
*************************************************************************
Everyday Italian
Harry Potter
*************************************************************************