Advertisement

07.01.2008 at 01:32AM PDT, ID: 23529397
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

Parsing Simple XML with Javascript

Asked by niko86 in JavaScript, Extensible Markup Language (XML), Asynchronous Javascript and XML (AJAX)

Tags: , ,

I recently began to learn Ajax, now im stuck with a little Application.
It sends a Post Request containing a path to a PHP Page, this Page returns a simple XML structure containing the Folders in the Directory specified by the path.

A sample of the XML can be found in the Code Section.

What i am trying to do is parsing the xml.
I want it to be displayed row by row, doesnt matter if a table is used for this or if there are just text lines seperated by "<br>"

This is what i got so far:Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
#html file
<html>
	<head>
		<script type="text/javascript" src="ajax.js"> </script>
		<script type="text/javascript" src="dir.js"> </script>
	</head>
	<body>
		<div id="main">
			<form id="pfad">
				<input type="text" name="pfad-eingabe" id="pfad-eingabe" >
/*Input for the Path we want to look up */
				<input type="button" name="submit" value="List Dir" onClick="listdir();" \>
/*Submit Button calls the function which sends the Path to the Server */
			</form>
			<div id="result">
/*Results should be here */
			</div>
		</div>
	</body>
</html>
 
#ajax.js
var anfrage = null;
try {
  anfrage = new XMLHttpRequest();
} catch (versuchmicrosoft) {
  try {
    anfrage = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (anderesmicrosoft) {
    try {
      anfrage = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (fehlschlag) {
      anfrage = null;
    }
  }
}
 
if (anfrage == null)
  alert("Fehler beim Erzeugen des Anfrage-Objekts!");
/* This is just to create the Object for Sending/Receiving the Data */
 
#dir.js
function listdir() {
  var pfad = document.getElementById("pfad-eingabe").value;
  var url = "list.php";
/* the server-side script */
  anfrage.open("POST", url, true);
/* send by Post */
  anfrage.onreadystatechange = updateSeite;
/* call Update Function */
  anfrage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  anfrage.send("pfad-eingabe=" + escape(pfad));
}
 
function updateSeite() {
	if (anfrage.readyState == 4) {
		if (anfrage.status == 200) {
		/* Antwort vom Server empfangen */
/* If we got correct response */
			var dirlist = anfrage.responseXML;
/* read XML */
			var dirlistText = anfrage.responseText;
/* for testing purposes, read the Text-Only Contents */
		        /* This is where I am stuck, what to do now? */
 
		} else
		alert("Fehler! Anfragestatus ist " + anfrage.status + " / " + anfrage.statusText);
	}
}
 
#PHP File, XML Sample
//Pfad
$pfad = $_REQUEST['pfad-eingabe'];
if (!isset($_REQUEST['pfad-eingabe'])) {
	header("pfad-eingabe nicht gesetzt", true, 400);
        echo " ";
	exit;
}
 
//opendir func
$dir_handle = @opendir($pfad) or die("Unable to open $pfad");
 
header("Content-Type: text/xml");
//send content header
echo '<?xml version=\"1.0\" encoding=\"utf-8\"?>';
echo "Directory Listing von ".$pfad;
echo "<listing>";
//running the while loop
$iter = "0";
while ($entry = readdir($dir_handle)) 
{
	echo "<entry id=".$iter.">";
	echo "$entry";
	echo "</entry>";
	$iter++;
}
echo "</listing>";
//closing the directory
closedir($dir_handle);
 
Loading Advertisement...
 
[+][-]07.03.2008 at 10:37AM PDT, ID: 21927557

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Extensible Markup Language (XML), Asynchronous Javascript and XML (AJAX)
Tags: Javascript/Ajax/XML, Firefox/IE, async load of xml, parse xml, view it
Sign Up Now!
Solution Provided By: Zyloch
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.03.2008 at 10:44AM PDT, ID: 21927614

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628