Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Importing XML using Javascript, same html file

Reply
Thread Tools

Importing XML using Javascript, same html file

 
 
dbsmilr@gmail.com
Guest
Posts: n/a
 
      04-12-2008
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!
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      04-12-2008
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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-12-2008
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>
<myxmlml>
<myxml:book title="book1" />
<myxml:book title="book1" />
</myxmlml>

<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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-12-2008
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>
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      04-17-2008
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
 
Reply With Quote
 
Pavel Lepin
Guest
Posts: n/a
 
      04-17-2008

Bart Van der Donck <> wrote in
<1d6abb47-76cb-4cb5-9ab7->:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any adv. in importing a module and some objects in the samemodule, into the same file? Visco Shaun Python 3 04-22-2009 06:13 AM
Two processes writing to the same XML file at the same time? darrel ASP .Net 2 04-05-2006 05:30 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM



Advertisments
 



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