Importing XML using Javascript, same html file

D

dbsmilr

This is what I want to do:

<html>
<xml id="myXml">
<book title="book1" />
<book title="book2" />
</xml>
<div id="out"><!-- I would output the xml formatted nicely for the
user in this div</div>

<script type="text/javascript">
var xmlDoc = document.getElementById("myXml");
alert(xmlDoc.hasChildNodes);
</script>
</html>

But that doesn't work, it says document.getElementById("xmlXml") has
no properties. How do I do a scenario like this? Thanks!
 
B

Bart Van der Donck

This is what I want to do:

<html>
  <xml id="myXml">
    <book title="book1" />
    <book title="book2" />
  </xml>
  <div id="out"><!-- I would output the xml formatted nicely for
the user in this div</div>

  <script type="text/javascript">
    var xmlDoc = document.getElementById("myXml");
    alert(xmlDoc.hasChildNodes);
  </script>
</html>

But that doesn't work, it says document.getElementById("xmlXml") has
no properties.  How do I do a scenario like this?  Thanks!

XML-data should always be approached with the XML-parser of the
browser. Here is a nice example of string parsing:

http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2

Hope this helps,
 
T

Thomas 'PointedEars' Lahn

This is what I want to do:

<html>
<xml id="myXml">
<book title="book1" />
<book title="book2" />
</xml>
<div id="out"><!-- I would output the xml formatted nicely for the
user in this div</div>

<script type="text/javascript">
var xmlDoc = document.getElementById("myXml");
alert(xmlDoc.hasChildNodes);
</script>
</html>

But that doesn't work, it says document.getElementById("xmlXml") has
no properties. How do I do a scenario like this?

The first thing you would need to make sure is Valid markup. The above is
not. document.getElementById() where `document' refers to an object
implementing the HTMLDocument interface finds *HTML* elements with
attributes of type ID; `xml' is not an HTML element.

You might have better luck with document.getElementsByTagName("xml")[0] and
continuing from there. But that does not make your markup Valid; to embed
XML in HTML, you have to use XHTML and namespaces, for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" [
<!ENTITY myxml "http://foo.example/bar">
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:myxml="&myxml;">
<head>
<title>Embedded XML Data</title>
</head>

<body>
<myxml:xml>
<myxml:book title="book1" />
<myxml:book title="book1" />
</myxml:xml>

<script type="text/javascript">
// works only with application/xhtml+xml
var xmlDoc = document.getElementsByTagNameNS("&myxml;", "xml");

// "[object NodeList]" in Gecko
window.alert(xmlDoc[0].childNodes);
</script>
</body>
</html>

That said, you should solve this with an external XML resource and XSLT,
preferably server-side, instead.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Bart said:
XML-data should always be approached with the XML-parser of the
browser. Here is a nice example of string parsing:

http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2

Hope this helps,

This unsurprisingly inefficient, error-prone, incompatible, invalid example
cannot help with the OP's problem since the OP is not getting at the invalid
`xml' element object to begin with, as the error message indicates (`null'
has no properties).

Using XSLT to transform the XML markup into HTML markup, and CSS to format
the HTML markup, is the proper way here, and if XSLT is used server-side it
degrades gracefully.

http://en.wikipedia.org/wiki/XSLT
http://developer.mozilla.org/en/docs/XSLT


PointedEars
 
B

Bart Van der Donck

Thomas said:
Bart Van der Donck wrote:

This unsurprisingly inefficient, error-prone, incompatible, invalid example
cannot help with the OP's problem since the OP is not getting at the invalid
`xml' element object to begin with, as the error message indicates (`null'
has no properties).

That javascript code is less optimal indeed. I think the main issue is
the bad exception handling (which involves feature detection too) on
several points. Also best to correct the errors of validator.w3.org.

In a strict sense, your error message hasn't anything to do with the
application. The XML-data should be valid and well-formed; that is
Ipso Facto the responsibility of the data itself and not from the
application that parses it (which of course doesn't mean that it
shouldn't be checked anymore).
Using XSLT to transform the XML markup into HTML markup, and CSS to format
the HTML markup, is the proper way here, and if XSLT is used server-side it
degrades gracefully.

http://en.wikipedia.org/wiki/XSLT
http://developer.mozilla.org/en/docs/XSLT

My experiences with XSLT are not very positive. I would avoid it.
 
P

Pavel Lepin

Bart Van der Donck said:
My experiences with XSLT are not very positive. I would
avoid it.

My experiences with XSLT were very positive. I wouldn't
avoid it.

Just a second opinion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top