473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1342
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Emil Karlen | last post by:
Using the import-tag, a template appearing in the imported file, can be extended in the importing file. I am extending a template this way and in the main-file, inside the template use...
11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
17
by: OdAwG | last post by:
Just some questions regarding tables. I am new Access Database and need a little help. I have the following data listed below 01. I have a table called tbl_Customer with the following...
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the...
6
by: rpjanaka | last post by:
Hi all, Instead of writing javascript functions on the html page it self, we could write those in separate “.js” file and import it as follow. <script src="fileName.js" ></script> But when...
1
by: Phil | last post by:
I want to add a button to a form to import a text delimited file. The File is delimited by "^" and so I have created a specification file by manually impoting the text file, using the advanced...
6
by: passionateforjava | last post by:
Hi All, I am using struts application wherein I need to import file for some purpose.I have used input type="file" for the same which goes like: <input type="file" id="uploadFile" name="uploadFile"...
5
by: phpmagesh | last post by:
Hi All, I have html page, in that i have some flash header. In that flash header i have sound on/off function. i implemented this in my html page. but i need some modification in this. now it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.