prain
asked on
Trouble Reading this XML file from javascript
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.X MLDOM");
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.tag Name);
alert("Num Nodes: " + xmlDoc.childNodes(0).valuO f);
alert("Num Nodes: " + xmlDoc.childNodes(1).hasCh ildNodes() );
alert("Num Nodes: " + xmlDoc.childNodes(2).hasCh ildNodes() );
}
}
</script>
I do not get the answers I expect. Can some one help?
<?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.X
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.tag
alert("Num Nodes: " + xmlDoc.childNodes(0).valuO
alert("Num Nodes: " + xmlDoc.childNodes(1).hasCh
alert("Num Nodes: " + xmlDoc.childNodes(2).hasCh
}
}
</script>
I do not get the answers I expect. Can some one help?
change to
function readXML() {
if (xmlDoc.readyState == 3) {
//Using documentElement Properties
//Output company
alert("XML Root Tag Name: " + xmlDoc.documentElement.tag Name);
alert("Num Nodes: " + xmlDoc.childNodes(0).valuO f);
alert("Num Nodes: " + xmlDoc.childNodes(1).hasCh ildNodes() );
alert("Num Nodes: " + xmlDoc.childNodes(2).hasCh ildNodes() );
}
}
</script>
function readXML() {
if (xmlDoc.readyState == 3) {
//Using documentElement Properties
//Output company
alert("XML Root Tag Name: " + xmlDoc.documentElement.tag
alert("Num Nodes: " + xmlDoc.childNodes(0).valuO
alert("Num Nodes: " + xmlDoc.childNodes(1).hasCh
alert("Num Nodes: " + xmlDoc.childNodes(2).hasCh
}
}
</script>
ASKER
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.tag Name);
}
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?
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.tag
}
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?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You. Works Great!
if (xmlDoc.readyState == 3