Solved

Working with xml on a web site.

Posted on 2014-12-08
4
113 Views
Last Modified: 2014-12-10
I am working with xml file dump from are ad server.

<?xml version="1.0" encoding="UTF-8"?>

<Information Technology>
	<user>

    	<Name>Jim Lin</Name>
    	<Title>Application and Systems Supp. Spec.</Title>
    	<Department>Information Technology</Department>
    	<Extension>5555</Extension>
    	<Mobile Number>555-555-55555</Mobile Number>
    	<Office Location>Sylvan</Office Location>
    	<Email Address>jlin@google.com</Email Address>
    	<Username>jlin</username>
    	<Manager>Bob Arm</Manager>

	</user>

	<user>

    	<Name>John Smith</Name>
    	<Title>Network Admin.</Title>
    	<Department>Information Technology</Department>
    	<Extension>6666</Extension>
    	<Mobile Number>666-666-6666</Mobile Number>
    	<Office Location>Sylvan</Office Location>
    	<Email Address>jsmith@google.com</Email Address>
    	<Username>jsmith</username>
    	<Manager>Bob Arm</Manager>

	<user>

</Information Technology>

Open in new window


You get the idea I want to display this info on a web page that runs locally on the users laptop. There is no web server on the laptop and it's in not connect to the internet.

It does have java installed. How can i have this info displayed in a web browser
0
Comment
Question by:armasmike
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
nap0leon earned 500 total points
ID: 40487774
Hopefully that is not the actual XML... way too many errors in it for it to work properly (mismatched case in the xml tags, spaces in the tag values, etc.).

To load the XML from a file:
http://www.w3schools.com/dom/dom_loadxmldoc.asp
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // code for IE5 and IE6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
} 
var xmlDoc=loadXMLDoc("books.xml");

Open in new window


To load XML from a string (in case you are importing it as text)
http://www.w3schools.com/dom/dom_loadxmldoc.asp
function loadXMLString(txt)
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // code for IE
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt);
  }
return xmlDoc;
}
var xmlDoc=loadXMLString(text);

Open in new window


To get values from the XML once you load it into a variable:
http://www.w3schools.com/dom/dom_methods.asp

Then you loop through it to get what you need.
e.g., to get the name of the first user:
function loadXMLString(txt)
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // code for IE
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt);
  }
return xmlDoc;
}

text= "<InformationTechnology>"
text+= "<user>"
text+= "<Name>Jim Lin</Name>"
text+= "<Title>Application and Systems Supp. Spec.</Title>"
text+= "<Department>Information Technology</Department>"
text+= "<Extension>5555</Extension>"
text+= "<MobileNumber>555-555-55555</MobileNumber>"
text+= "<OfficeLocation>Sylvan</OfficeLocation>"
text+= "<EmailAddress>jlin@google.com</EmailAddress>"
text+= "<Username>jlin</Username>"
text+= "<Manager>Bob Arm</Manager>"
text+= "</user>"
text+= "<user>"
text+= "<Name>John Smith</Name>"
text+= "<Title>Network Admin.</Title>"
text+= "<Department>Information Technology</Department>"
text+= "<Extension>6666</Extension>"
text+= "<MobileNumber>666-666-6666</MobileNumber>"
text+= "<OfficeLocation>Sylvan</OfficeLocation>"
text+= "<EmailAddress>jsmith@google.com</EmailAddress>"
text+= "<Username>jsmith</Username>"
text+= "<Manager>Bob Arm</Manager>"
text+= "</user>"
text+= "</InformationTechnology>"

var xmlDoc=loadXMLString(text);


xmlDoc.getElementsByTagName("InformationTechnology")[0].getElementsByTagName("user")[0].getElementsByTagName("Name")[0].childNodes[0].nodeValue

Open in new window

0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40487860
With XSLT you can transform an XML document into HTML, no web server needed but some CSS knowledge is necessary.
see http://www.w3schools.com/xml/xml_xsl.asp for an example
0
 

Author Closing Comment

by:armasmike
ID: 40489357
thank you
0
 
LVL 18

Expert Comment

by:nap0leon
ID: 40491177
armasmike - if you are not limited to using only JavaScript, you should definitely look into using XSLT as Walter suggested.  

There's a bit of a learning curve, especially if you want to do anything super fancy with it, but it may present additional flexibility in presenting the information on your page.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now