473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I extract data from XML

15
Hi,

I quite new to Javascript and XML. Here a is snippet of my XML file.

Expand|Select|Wrap|Line Numbers
  1. <xml>
  2. <cd>
  3.     <rock>
  4.         <item id='1'>
  5.             <name>Bon Jovi</name>
  6.             <price>$10</price>
  7.             <description>abc</description>
  8.         </item>
  9.         <item id='2'>
  10.             <name>U2</name>
  11.             <price>$11</price>
  12.             <description>abc</description>
  13.         </item>
  14.         <item id='3'>
  15.             <name>Linkin Park</name>
  16.             <price>$12</price>
  17.             <description>abc</description>
  18.         </item>
  19.     </rock>
  20.     <pop>
  21.         <item id='1'>
  22.             <name>Nelly</name>
  23.             <price>$5</price>
  24.             <description>abc</description>
  25.         </item>
  26.         <item id='2'>
  27.             <name>Justin</name>
  28.             <price>$5</price>
  29.             <description>abc</description>
  30.         </item>
  31.     </pop>
  32. </cd>
  33. </xml>
  34.  
I am working with javascript.
Basically I have 2 problems.
1. How do I just get the 'name' from all the items in <rock> tags?
2. How do I get the the 'name' from a specific position node? For example, I want to get the name of the artist in <rock> from node 1 onwards. -> Expected output: U2, Linkin Park.

Thanks so much in advance.

Regards,
Gordon
Oct 24 '07 #1
10 11974
dmjpro
2,476 2GB
actually where this file exists??

debasis
Oct 24 '07 #2
acoder
16,027 Expert Mod 8TB
Gordon, welcome to TSDN!

Check out this link. You'll want to make use of the document.getElementsByTagName() method.
Oct 24 '07 #3
spoken
15
Hi acoder,

I've seen that link. However, I just want to get the 'name' for the items in <rock>.
I have tried
xmldoc.getElementsByTagName('items')
but that gives me all the items, including those in <pop>.

dmjpro,
the file is a seperate xml and the javascript is in my html document. I manage to successfully load the xml already.
Oct 24 '07 #4
dmjpro
2,476 2GB
Hi acoder,

I've seen that link. However, I just want to get the 'name' for the items in <rock>.
I have tried
xmldoc.getElementsByTagName('items')
but that gives me all the items, including those in <pop>.

dmjpro,
the file is a seperate xml and the javascript is in my html document. I manage to successfully load the xml already.

have a look at this ............

Expand|Select|Wrap|Line Numbers
  1. var parent = xmldoc.getElementsByTagName('rock'); //get the parent node
  2. if(parent[0].childNodes.length){
  3. var child1 = parent[0].childNodes[0].getAttribute('name'); //if there is any child nodes
  4. }
  5.  
have the same repetition ...... until the childnodes end.

debasis
Oct 24 '07 #5
spoken
15
have a look at this ............

Expand|Select|Wrap|Line Numbers
  1. var parent = xmldoc.getElementsByTagName('rock'); //get the parent node
  2. if(parent[0].childNodes.length){
  3. var child1 = parent[0].childNodes[0].getAttribute('name'); //if there is any child nodes
  4. }
  5.  
have the same repetition ...... until the childnodes end.

debasis

Hi debasis,

Thanks for your reply.
I've tried your code and I can't get any output. This is wat I wrote.

Expand|Select|Wrap|Line Numbers
  1. var parent = xmldoc.getElementsByTagName('rock'); //get the parent node
  2. alert(parent);
  3. if(parent[0].childNodes.length){
  4.     var child1 = parent[0].childNodes[0].getAttribute('name'); //if there is any child nodes
  5. }
  6. alert(child1);
I get "HTMLCollection" for the first alert and nothing comes out for the second, and I'm using the exact XML file as above. I tried to print the whole xml file and it comes out fine.
Oct 24 '07 #6
gits
5,390 Expert Mod 4TB
hi ...

try the following adaption:

Expand|Select|Wrap|Line Numbers
  1. var p = xmldoc.getElementsByTagName('rock');
  2.  
  3. alert(p);
  4.  
  5. if (p[0].childNodes.length) {
  6.     var child1 = p[0].getElementsByTagName('item')[0];
  7.     var c_name = child1.getAttribute('name'); 
  8. }
  9.  
  10. alert(c_name);
kind regards
Oct 24 '07 #7
spoken
15
hi ...

try the following adaption:

Expand|Select|Wrap|Line Numbers
  1. var p = xmldoc.getElementsByTagName('rock');
  2.  
  3. alert(p);
  4.  
  5. if (p[0].childNodes.length) {
  6.     var child1 = p[0].getElementsByTagName('item')[0];
  7.     var c_name = child1.getAttribute('name'); 
  8. }
  9.  
  10. alert(c_name);
kind regards
Hi Gits,

I tried your code adaption. I can't do child1.getAttribute('name') because name is not an attribute but a tag. However if I do child1.getAttribute('id'), I can extract the ID of the item.

But I need to get the value of the <name> tag.. :(

This is driving me bonkes.. Thanks for the help...

Regards,
Gordon
Oct 24 '07 #8
acoder
16,027 Expert Mod 8TB
Hi Gits,

I tried your code adaption. I can't do child1.getAttribute('name') because name is not an attribute but a tag. However if I do child1.getAttribute('id'), I can extract the ID of the item.
So, use getElementsByTagName() again on child1 (which is item) to get the name. That would give you the first name. Use a loop to get all the names of all items.
Oct 24 '07 #9
Ferris
101 100+
Hi

I just wrote a complete sample code for you~~ It takes me a little long time..

first:save your xml data to file :cd.xml
second:create a new html file, make sure these two files are in the same directory.
then:paste my code into html file.

end:open html file and click the button.


[HTML]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="javascript">
function readxml(xmlfile)
{
var xmlDoc;
if (window.ActiveXObject){
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
}
else if (document.implementation && document.implementation.createDocument){
xmlDoc= document.implementation.createDocument("","doc",nu ll);
}
if (typeof xmlDoc!="undefined"){
xmlDoc.load(xmlfile);
}
if (window.ActiveXObject)
{
//for IE
var rock = xmlDoc.getElementsByTagName("rock")[0];
if (rock != null)
{
var rockitem = rock.getElementsByTagName("item");
for (var i=0;i<rockitem.length;i++)
{
var id = rockitem[i].getAttribute("id");

var name = rockitem[i].getElementsByTagName("name")[0].firstChild.nodeValue;
var price = rockitem[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var description = rockitem[i].getElementsByTagName("description")[0].firstChild.nodeValue;
document.getElementById("divrock").innerHTML += "item " + i + "<br />";
document.getElementById("divrock").innerHTML += "name:" + name + "<br />";
document.getElementById("divrock").innerHTML += "price:" + price + "<br />";
document.getElementById("divrock").innerHTML += "description:" + description + "<br />";
}
}
var pop = xmlDoc.getElementsByTagName("pop")[0];
if (pop != null)
{
var popitem = pop.getElementsByTagName("item");
for (var i=0;i<popitem.length;i++)
{
var id = popitem[i].getAttribute("id");

var name = popitem[i].getElementsByTagName("name")[0].firstChild.nodeValue;
var price = popitem[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var description = popitem[i].getElementsByTagName("description")[0].firstChild.nodeValue;
document.getElementById("divpop").innerHTML += "item " + i + "<br />";
document.getElementById("divpop").innerHTML += "name:" + name + "<br />";
document.getElementById("divpop").innerHTML += "price:" + price + "<br />";
document.getElementById("divpop").innerHTML += "description:" + description + "<br />";
}
}
}
else if (typeof xmlDoc!="undefined"){
// for firefox
xmlDoc.onload = function (){
var rock = xmlDoc.getElementsByTagName("rock")[0];
if (rock != null)
{
var rockitem = rock.getElementsByTagName("item");
for (var i=0;i<rockitem.length;i++)
{
var id = rockitem[i].getAttribute("id");

var name = rockitem[i].getElementsByTagName("name")[0].firstChild.nodeValue;
var price = rockitem[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var description = rockitem[i].getElementsByTagName("description")[0].firstChild.nodeValue;
document.getElementById("divrock").innerHTML += "<strong>item</strong> " + i + "<br />";
document.getElementById("divrock").innerHTML += "<strong>name</strong>:" + name + "<br />";
document.getElementById("divrock").innerHTML += "<strong>price</strong>:" + price + "<br />";
document.getElementById("divrock").innerHTML += "<strong>description</strong>:" + description + "<br />";
}
}
var pop = xmlDoc.getElementsByTagName("pop")[0];
if (pop != null)
{
var popitem = pop.getElementsByTagName("item");
for (var i=0;i<popitem.length;i++)
{
var id = popitem[i].getAttribute("id");

var name = popitem[i].getElementsByTagName("name")[0].firstChild.nodeValue;
var price = popitem[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var description = popitem[i].getElementsByTagName("description")[0].firstChild.nodeValue;
document.getElementById("divpop").innerHTML += "<strong>item</strong> " + i + "<br />";
document.getElementById("divpop").innerHTML += "<strong>name</strong>:" + name + "<br />";
document.getElementById("divpop").innerHTML += "<strong>price</strong>:" + price + "<br />";
document.getElementById("divpop").innerHTML += "<strong>description</strong>:" + description + "<br />";
}
}
};
}
}
</script>
<body>
<h1>read xml file</h1>
rock
<div id="divrock" style="margin-left:10px; "></div>
pop
<div id="divpop" style="margin-left:10px; "></div>
<input type="button" value="read" onclick="readxml('cd.xml');"/>
</body>
</html>

[/HTML]



by the way:
1:the code can be run in IE and firefox.
2:pay attention to onclick="readxml('cd.xml'); , the "cd.xml" will tell javascript where your xml file is.if your xml file isn't "cd.xml",change the parameter.
3:pay attention to the following code,I guess these code are what you're looking for:

[HTML]
var rockitem = rock.getElementsByTagName("item");
for (var i=0;i<rockitem.length;i++)
{
var id = rockitem[i].getAttribute("id");
var name =
rockitem[i].getElementsByTagName("name")[0].firstChild.nodeValue;
var price =
rockitem[i].getElementsByTagName("price")[0].firstChild.nodeValue;
var description =
rockitem[i].getElementsByTagName("description")[0].firstChild.nodeValue;
}
[/HTML]


hope it helps :)
Oct 24 '07 #10
spoken
15
Wow..
Thank you so much Ferris.. I was going around in endless circles trying to transverse my XML file.

I guess the code that I didn't manage to get was:

Expand|Select|Wrap|Line Numbers
  1.       var p = xmldoc.getElementsByTagName('rock');
  2.       if (p[0].childNodes.length) {
  3.           var child1 = p[0].getElementsByTagName('item')[0]; //child1 => item 0
  4.           var c_name = child1.getElementsByTagName('name')[0].firstChild.nodeValue;          
  5.       }
  6.  
Thanks again for your effort. I think I'm getting it a lil better now..

Regards,
Gordon
Oct 24 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tim Smith | last post by:
I am looking to extract form element values from html, more generally I have a substring that identifies the beginning of a value and a string that identifies the end of value and I need to extract...
0
by: Brian Hanson | last post by:
Hi, I have an unusual problem that just showed its ugly head at a pretty bad time. I have an asp.net (VB) app that takes data from an Excel sheet and puts it into SQL Server. I get the data...
7
by: fool | last post by:
Dear group, Extract the integer value present in a given string. So I tried the following: int main(void) { int val; char *data; data = malloc(sizeof *data); if(data)
2
by: missolsr | last post by:
hi, I am using jpcap to capture OLSR topology control (udp) packets. Does anyone know how to extract data (the way ethereal does it) from the olsr packet? There are methods to extract data...
1
by: caine | last post by:
I want to extract web data from a news feed page http://everling.nierchi.net/mmubulletins.php. Just want to extract necessary info between open n closing tags of <title>, <categoryand <link>....
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
3
by: maylee21 | last post by:
hi, anyone can help me figure out how to read data from a text file like this: 10980012907200228082002 and extract the data according to this kind of format: Record type 1 TY-RECORD ...
5
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements...
7
by: JoeC | last post by:
I am trying to create a windows program that reads binary graphics as a resource. This has nothing to do with win32 but conversion of data with memcpy. graphic::graphic(UINT uiResID, HINSTANCE...
1
by: sivadhanekula | last post by:
Hi everyone, I have a problem with my Mysql data. I have 2,95,67,456 lines of data which is too much and if I run this in MYSQL front-end it is telling "OUT OF MEMORY". Any way with my collegues...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.