Here i show the example for parsing xml data, using jquery, javascript and php.
Here is a sample xml file, we are using, msg.xml:
<messages>
<message>
<name>Sam</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Kelly</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Sam</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Sam</name>
<msg>Hello Anna</msg>
</message>
<message>
<name>Kelly</name>
<msg>Anna</msg>
</message>
</messages>
The source code:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
xmlDoc=loadXMLDoc("msg.xml");
x=xmlDoc.getElementsByTagName("name")[0];
y=x.childNodes[0];
document.write(y.nodeValue);
document.write("<br>");
var xml = "<music><album>Michael Jackson</album></music>";
var result = $(xml).find("album").text();
document.write(result);
document.write("<br>");
</script>
</head>
<body>
<?php
$xml = "<books>
<book>Patterns of Enterprise Application Architecture</book>
<book>Design Patterns: Elements of Reusable Software Design</book>
<book>Clean Code</book>
</books>";
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
echo $book->nodeValue.'<br>';
}
?>
</body>
</html>
Output:
Sam
Michael Jackson
Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
Here is a sample xml file, we are using, msg.xml:
<messages>
<message>
<name>Sam</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Kelly</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Sam</name>
<msg>Hi Anna</msg>
</message>
<message>
<name>Sam</name>
<msg>Hello Anna</msg>
</message>
<message>
<name>Kelly</name>
<msg>Anna</msg>
</message>
</messages>
The source code:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
xmlDoc=loadXMLDoc("msg.xml");
x=xmlDoc.getElementsByTagName("name")[0];
y=x.childNodes[0];
document.write(y.nodeValue);
document.write("<br>");
var xml = "<music><album>Michael Jackson</album></music>";
var result = $(xml).find("album").text();
document.write(result);
document.write("<br>");
</script>
</head>
<body>
<?php
$xml = "<books>
<book>Patterns of Enterprise Application Architecture</book>
<book>Design Patterns: Elements of Reusable Software Design</book>
<book>Clean Code</book>
</books>";
$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
echo $book->nodeValue.'<br>';
}
?>
</body>
</html>
Output:
Sam
Michael Jackson
Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
Comments
Post a Comment