473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Parsing Newbie Madness

I've been instructing myself in XML DOM parsing using the w3schools
tutorial and decided to try an example of my own. I'd written a short
XML file that looked like this:

<?xml version="1.0" encoding="UTF-8"?>
<apartment>
<tenant>
<name>
<first>John</first>
<last>Smith</last>
</name>
<age>23</age>
<occupation>Stu dent</occupation>
</tenant>
<tenant>
<name>
<first>Alan</first>
<last>Smithee </last>
</name>
<age>22</age>
<occupation>Ser ver</occupation>
</tenant>
<tenant>
<name>
<first>Jane</first>
<last>Smith</last>
</name>
<age>34</age>
<occupation>Man ager</occupation>
</tenant>
</apartment>

I wanted to parse the xml dom with javascript and insert node values
into html elements at load calling the following function:

function parseXML()
{
xmlDoc = document.implem entation.create Document("","", null);
xmlDoc.async = "false";
xmlDoc.load("Ap artment.xml");

document.getEle mentById("fname ").innerHTM L =
xmlDoc.getEleme ntsByTagName("n ame")[0].childNodes[0].nodeValue;
}

When I tested to see the resulting output in Safari and Firefox, I
received to different error messages:

1) Safari 3.1's Inspector error console told me: value undefined Value
undefined (result of expression xmlDoc.load) is not object.
2) Firebug in Firefox told me: xmlDoc.getEleme ntsByTagName("n ame")[0]
has no properties

I decided to ignore Safari and focus on the Firefox bug first. I
deleted the last statement of the function just to make sure the xml
file was being loaded. However, no matter where I parsed the tree for
a node value, I was told the node had no value. So I decided to
validate my XML using the XML Developer extension for Firefox and ran
the XML through its validator (it used some default scheme when none
was provided) and I was told that:

cvc-elt.1: Cannot find the declaration of element 'apartment'.

Huh? But its right there! Am I doing something wrong?
Jun 27 '08 #1
5 1575
pr
Benoit wrote:
I've been instructing myself in XML DOM parsing using the w3schools
tutorial and decided to try an example of my own. I'd written a short
XML file that looked like this:
[...]

It checks out.
>
I wanted to parse the xml dom with javascript and insert node values
into html elements at load calling the following function:

function parseXML()
{
xmlDoc = document.implem entation.create Document("","", null);
Don't forget *var*.
xmlDoc.async = "false";
I don't believe Firefox lets you do this, so you need an event listener
to intercept a successful load(), or to use an XMLHttpRequest.
xmlDoc.load("Ap artment.xml");

document.getEle mentById("fname ").innerHTM L =
xmlDoc.getEleme ntsByTagName("n ame")[0].childNodes[0].nodeValue;
That isn't quite right. nodeValue only applies to text nodes. Firefox
2.x (at least) supports textContent on element nodes, however.

Putting it all together, you can replace those two lines with:

xmlDoc.addEvent Listener("load" , loaded, false);

function loaded(e) {
document.getEle mentById("fname ").innerHTM L =
xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;
}

xmlDoc.load("Ap artment.xml");
}

When I tested to see the resulting output in Safari and Firefox, I
received to different error messages:

1) Safari 3.1's Inspector error console told me: value undefined Value
undefined (result of expression xmlDoc.load) is not object.
2) Firebug in Firefox told me: xmlDoc.getEleme ntsByTagName("n ame")[0]
has no properties

I decided to ignore Safari and focus on the Firefox bug first. I
deleted the last statement of the function just to make sure the xml
file was being loaded. However, no matter where I parsed the tree for
a node value, I was told the node had no value. So I decided to
validate my XML using the XML Developer extension for Firefox and ran
the XML through its validator (it used some default scheme when none
was provided) and I was told that:

cvc-elt.1: Cannot find the declaration of element 'apartment'.

Huh? But its right there! Am I doing something wrong?
You don't want to get into serious XML validation at this point, believe
me :-)
Jun 27 '08 #2
pr
pr wrote:
Benoit wrote:
> xmlDoc.async = "false";

I don't believe Firefox lets you do this, so you need an event listener
to intercept a successful load(), or to use an XMLHttpRequest.
Argh, you confused me for a minute there. Of course async doesn't work
if it's a string value. What you wanted was:

xmlDoc.async = false;
Jun 27 '08 #3
pr
VK wrote:
On Apr 13, 1:23 am, pr <p...@porl.glob alnet.co.ukwrot e:
> xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;

Right, I forgot that DOM 3 finally introduced .textContent as
equivalent to IE's .text and it is finally mostly supported. So the
cross-browser code might be like:
[...]

For that simple example maybe. For serious purposes you would IMO use
XMLHttpRequest asynchronously, supply an alternative to textContent/text
and avoid innerHTML. Safari 3.1 wouldn't have any difficulty with that.

Jun 27 '08 #4
Seeing as the W3schools tutorial is misleading, could suggest a text
on XML technologies that's modern and browser-agnostic if not browser-
sensitive?

On Apr 12, 5:44 pm, pr <p...@porl.glob alnet.co.ukwrot e:
VK wrote:
On Apr 13, 1:23 am, pr <p...@porl.glob alnet.co.ukwrot e:
xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;
Right, I forgot that DOM 3 finally introduced .textContent as
equivalent to IE's .text and it is finally mostly supported. So the
cross-browser code might be like:

[...]

For that simple example maybe. For serious purposes you would IMO use
XMLHttpRequest asynchronously, supply an alternative to textContent/text
and avoid innerHTML. Safari 3.1 wouldn't have any difficulty with that.
Jun 27 '08 #5
pr
Benoit wrote:
Seeing as the W3schools tutorial is misleading, could suggest a text
on XML technologies that's modern and browser-agnostic if not browser-
sensitive?
Here are a few links:

http://developer.mozilla.org/en/docs...etting_Started
http://developer.mozilla.org/en/docs/XMLHttpRequest
http://developer.mozilla.org/en/docs..._DOM_Reference

http://jibbering.com/2002/4/httprequest.html

On the subject of the version 2.0 DOM (for navigating and updating HTML
& XML content), there's nothing better than:

http://www.w3.org/TR/DOM-Level-2-Core

(once you get over the 'officialese').

You could also look at the Apple site, for example:

http://developer.apple.com/internet/...ent/dom2i.html

Microsoft has some good documentation on its MSXML SDK:

http://msdn2.microsoft.com/en-us/library/ms760399.aspx

but you have to bear in mind that IE doesn't support XHTML and therefore
interaction between HTML and XML documents in IE is carried out by
rather primitive means (innerHTML instead of importNode(), for example).

Hope that helps.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2984
by: ArdGre | last post by:
HI THERE, I have a strange problem. I am writing a firefox extension to the Onlywire API (http://onlywire.com/index?api). Now the problem is when I tag pages using the onlywire API the service responds with an xml that indicates whether the web page was tagged successfully. Something like this: <?xml version="1.0"?> <result code="success" />
1
1142
by: laptopia | last post by:
Here is the scenario: The data that needs to parsed is on an asp page secured by a login form. I have access to the site, but I was wondering how one would go about writing a script that would automatically access the page and grab the HTML. Garbing the HTML then parsing the information I understand. I just don't know if its possible to access the data through an external script (run from another server) even though we have access...
11
11228
by: saril.ks | last post by:
Hi The JSON object I am using is as below . this object is returned after an AJAX call {"application" :} In the JS i am using the below code data = ajaxRequest.responseText ;
5
4931
by: amjadcsu | last post by:
I am a newbie in python I am trying to parse a xml file and write its content in a txt file. The txt contains null elements. Any reason what iam doing wrong here Here is the code that i wrote import sys,os import xml.sax import xml.sax.handler
1
2521
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to start it and what main topics in C I will need to cover in the assignment. Thanks a lot. CSC180 Assignment #3: Menu Madness Due Date: Thursday, November 8th at 1:00am Contents: · General Info · What to Hand In o Submission Instructions
3
4248
by: Phillip B Oldham | last post by:
Hi. I'm stretching my boundaries in programming with a little python shell-script which is going to loop through a list of domain names, grab the whois record, parse it, and put the results into a csv. I've got the results coming back fine, but since I have *no* experience with python I'm wondering what would be the preferred "pythonic" way of parsing the whois string into a csv record. Tips/thoughts/examples more than welcome!
1
3420
by: Rick Owen | last post by:
Greetings, I have a form that, when submitted, calls a plsql procedure. The form has a number of fields (text, hidden, select, radio) but the particular field that is giving me problems is a <selectwhich allows multiple selection. When I receive the form values in my procedure I getting only the first value of those that are selected. For example if I select 1000, 1100, 1200, 1300 in the list I only get 1000 in my procedure. If I...
28
2200
by: pereges | last post by:
Hi I've a string input and I have to parse it in such a way that that there can be only white space till a digit is reached and once a digit is reached, there can be only digits or white space till the string ends. Am I doing this correctly ? : Code: #include <stdio.h> #include <string.h>
20
2932
by: Steve | last post by:
With the help of this newsgroup and Google I have got this code working fully in Firefox and can alert the XML in IE but because IE does not impliment the DOM "getElementsByTagNameNS()" function I cannot read the individual rates from the Cube namespace. Is there a wrapper or some other relatively simple method of getting IE to do what in Firefox is straighforward? Here is the code. Any help gratefully received.
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.