473,558 Members | 3,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("myXml ");
alert(xmlDoc.ha sChildNodes);
</script>
</html>

But that doesn't work, it says document.getEle mentById("xmlXm l") has
no properties. How do I do a scenario like this? Thanks!
Jun 27 '08 #1
5 1352
dbsm...@gmail.c om 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.getEle mentById("myXml ");
* * alert(xmlDoc.ha sChildNodes);
* </script>
</html>

But that doesn't work, it says document.getEle mentById("xmlXm l") 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.c om 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.getEle mentById("myXml ");
alert(xmlDoc.ha sChildNodes);
</script>
</html>

But that doesn't work, it says document.getEle mentById("xmlXm l") 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.getEle mentById() 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.getEle mentsByTagName( "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="&m yxml;">
<head>
<title>Embedd ed 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.getEle mentsByTagNameN S("&myxml;", "xml");

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

That said, you should solve this with an external XML resource and XSLT,
preferably server-side, instead.
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jun 27 '08 #3
Bart Van der Donck wrote:
dbsm...@gmail.c om 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.getEle mentById("myXml ");
alert(xmlDoc.ha sChildNodes);
</script>
</html>

But that doesn't work, it says document.getEle mentById("xmlXm l") 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.or g.

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.co mwrote in
<1d************ *************** *******@m44g200 0hsc.googlegrou ps.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
2083
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 "<apply-imports/> to trigger the template in the imported file. Now, the imported template takes a parameter. How can I pass a parameter to the template I am...
11
3389
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 the last field, it only checks the top few 'cells' to see if there is any data, if not, the field is not imported). How do I 'force' Access to...
29
4192
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 System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
17
2012
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 information listed below 02. In that table I have three columns that have numeric data in it (City, State, Marital_Status) 03. I need to convert the...
12
6192
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 datagrid. Once I can get to that point I need some way to be able to add new data only to the new columns that were added. Here is some of my...
6
1789
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 we do this and check the web page source on the browser, there is no any JavaScript function there.. But those functions are running without any...
1
1892
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 button and saving the specification. Naively I thought I just needed to add the specification file name to variable strSpec for the following...
6
4111
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" size="50"> I have the import button on which I have used onClick event to call javascript function submitValues() used to validate all the fields...
5
2298
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 works fine. if i switch off the flash sound sound gets switch off. But if i again refresh page or navigate to another page falsh sound gets switch on...
0
7550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7836
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7915
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5455
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3603
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1165
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
872
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.