Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing XML in Javascript

 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have a Javascript variable which holds an XML string.
var replyXML = <Name>
<First>Maya</First>
<Last>Menon</Last>
</Name>
I want to extract the values of First and Last and Save it into two Javascript variables.
How to do that ?
I have heard that using DHTML this can be done .. Any help

Can we use parser in Javascript ? If so which is the best parser to use ?
Thanks,
Maya


[This message has been edited by maya menon (edited August 21, 2001).]
 
David Weitzman
Ranch Hand
Posts: 1365
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Transforming XML to HTML on the client
Here is the simple source code needed to transform the XML file to HTML on the client (try it yourself):
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML document into memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The last line of code transforms the XML document using the XSL document, and writes the result to the HTML document.
Cheers
 
Tom George
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems the code you've posted will work only with IE. I'm looking for javascript syntax that can parse xml, across browsers. Is that a mirage??
:-)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amazing what you can find with a Google search - for example
this cross browser javascript parser project on sourceforge.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic