469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Importing XML using Javascript, same html file

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!
Jun 27 '08 #1
5 1233
dbsm...@gmail.com wrote:
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.a...ml_parsertest2

Hope this helps,

--
Bart
Jun 27 '08 #2
db*****@gmail.com wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #3
Bart Van der Donck wrote:
dbsm...@gmail.com wrote:
><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.a...ml_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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #4
Thomas 'PointedEars' Lahn wrote:
Bart Van der Donck wrote:
>http://www.w3schools.com/xml/tryit.a...ml_parsertest2
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.

--
Bart
Jun 27 '08 #5

Bart Van der Donck <ba**@nijlen.comwrote in
<1d**********************************@m44g2000hsc. googlegroups.com>:
Thomas 'PointedEars' Lahn wrote:
>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.
My experiences with XSLT were very positive. I wouldn't
avoid it.

Just a second opinion.

--
I'm not dead, just pinin' for the fnords.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Emil Karlen | last post: by
11 posts views Thread by Grim Reaper | last post: by
29 posts views Thread by Natan | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.