?
Solved

Trouble Reading this XML file from javascript

Posted on 2011-09-05
6
Medium Priority
?
378 Views
Last Modified: 2012-08-13
I have the following XML file...
<?xml version="1.0" encoding="UTF-8" ?>
<company>
  <employee id="001" sex="M" age="19" name="A"></employee>
  <employee id="002" sex="M" age="24" name="B"></employee>
  <employee id="003" sex="M" age="21" name="C"></employee>
</company>

Above file name is "employee.xml" on the server.
All I want is
1. To find how many employee elements we have
2. To number of employee elements, loop it and pick up each ones id', sex, age, name and display (for now an alert message is fine)

I want to write a piece of AJAX to do this.
So I wrote.,...

<script type="text/javascript">
        var xmlDoc;
        function loadXML() {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.onreadystatechange = readXML;
            xmlDoc.load("employees.xml");
        }

        function readXML() {
            if (xmlDoc.readyState == 4) {
                //Using documentElement Properties
                //Output company
                alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
                alert("Num Nodes: " + xmlDoc.childNodes(0).valuOf);
                alert("Num Nodes: " + xmlDoc.childNodes(1).hasChildNodes());
                alert("Num Nodes: " + xmlDoc.childNodes(2).hasChildNodes());
               
            }
        }
</script>

I do not get the answers I expect. Can some one help?
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 36484709
function readXML() {
            if (xmlDoc.readyState == 3
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 36484713
change to

 function readXML() {
            if (xmlDoc.readyState == 3) {
                //Using documentElement Properties
                //Output company
                alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
                alert("Num Nodes: " + xmlDoc.childNodes(0).valuOf);
                alert("Num Nodes: " + xmlDoc.childNodes(1).hasChildNodes());
                alert("Num Nodes: " + xmlDoc.childNodes(2).hasChildNodes());
               
            }
        }
</script>

 
0
 

Author Comment

by:prain
ID: 36484814
Gets a runtime error when changed to
if (xmlDoc.readyState == 3) {
  ....
}

Well when
if (xmlDoc.readyState == 4) {
//This line works and gives me "company" as the answer
alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
}

My problem is
1. How to get the # of "employee" elements
2. Then how to loop thru that number of elements and pick each elelment's "id", "sex", "age" and "name" components?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36484887
Plusone - in what universe is readystate 3 useful?

LOADING (1)
The load is in progress. Reading persisted properties, but not yet parsing data. For readyState definitions, data should be considered equivalent to binary large object (BLOB) properties.

LOADED (2)
Reading of the persisted properties completed. Reading and parsing data, but the object model is not yet available.

INTERACTIVE (3)      
Some data has been read and parsed, and the object model is now available on the partially retrieved data set. Although the object model is available during this state, it is read-only.

COMPLETED (4)      
The document has been completely loaded, successfully or unsuccessfully.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1000 total points
ID: 36484900
Assuming

<?xml version="1.0" encoding="UTF-8" ?>
<company>
  <employee id="001" sex="M" age="19" name="A"></employee>
  <employee id="002" sex="M" age="24" name="B"></employee>
  <employee id="003" sex="M" age="21" name="C"></employee>
</company>

you have

var emps = xmldoc.getElementsByTagName('employee');
var numberOfEmps = emps.length;
var gender,age,name;
for (var i=0;i<numberOfEmps;i++) {
    name=emps[i].getAttribute("name");
    gender=emps[i].getAttribute("sex");
    age=emps[i].getAttribute("age");
    alert(name +' aged '+age+' is '+gender)
}

Open in new window

0
 

Author Closing Comment

by:prain
ID: 36486206
Thank You. Works Great!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question