xml 101

M

mk834tt

Low hour xml/javascript pilot here. At http://www.w3schools.com/xml/xml_to_html.asp
trying to get an xml file to render in ff2. The problem is table
cells display "value </td>". If I remove the end tag, "< /td>" from
document.write (see lines 9 and 10) the text is clean and the end tag
is placed properly. Does document.write() have some smarts? I am
assuming it does unless someone can spot my error. Thank you.

Here's the code of interest:


01 for (var i = 0; i < cd.length; i++) {
02 var title = cd.getElementsByTagName("TITLE")
[0].childNodes[0].nodeValue;
03 var artist = cd.getElementsByTagName("ARTIST")
[0].childNodes[0].nodeValue;
04 var country = cd.getElementsByTagName("COUNTRY")
[0].childNodes[0].nodeValue;
05 var company = cd.getElementsByTagName("COMPANY")
[0].childNodes[0].nodeValue;
06 var price = cd.getElementsByTagName("PRICE")
[0].childNodes[0].nodeValue;
07 var year = cd.getElementsByTagName("YEAR")
[0].childNodes[0].nodeValue;
08 document.write("<tr>");
09 // document.write("<td>" + title + " < /td>");
10 document.write("<td>" + title);
11 document.write("<td>" + artist + " < /td>");
12 document.write("<td>" + country + " < /td>");
13 document.write("<td>" + company + " < /td>");
14 document.write("<td>" + price + " < /td>");
15 document.write("<td>" + year + " < /td>");
16 document.write("< /tr>");
17 }

Here's the first part of the xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>

<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
 
P

Patient Guy

Low hour xml/javascript pilot here. At
http://www.w3schools.com/xml/xml_to_html.asp trying to get an xml file
to render in ff2. The problem is table cells display "value </td>".
If I remove the end tag, "< /td>" from document.write (see lines 9 and
10) the text is clean and the end tag is placed properly. Does
document.write() have some smarts? I am assuming it does unless
someone can spot my error. Thank you.

Here's the code of interest:


01 for (var i = 0; i < cd.length; i++) {
02 var title = cd.getElementsByTagName("TITLE")
[0].childNodes[0].nodeValue;
03 var artist = cd.getElementsByTagName("ARTIST")
[0].childNodes[0].nodeValue;
04 var country = cd.getElementsByTagName("COUNTRY")
[0].childNodes[0].nodeValue;
05 var company = cd.getElementsByTagName("COMPANY")
[0].childNodes[0].nodeValue;
06 var price = cd.getElementsByTagName("PRICE")
[0].childNodes[0].nodeValue;
07 var year = cd.getElementsByTagName("YEAR")
[0].childNodes[0].nodeValue;
08 document.write("<tr>");
09 // document.write("<td>" + title + " < /td>");
10 document.write("<td>" + title);
11 document.write("<td>" + artist + " < /td>");
12 document.write("<td>" + country + " < /td>");
13 document.write("<td>" + company + " < /td>");
14 document.write("<td>" + price + " < /td>");
15 document.write("<td>" + year + " < /td>");
16 document.write("< /tr>");
17 }

Here's the first part of the xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>

<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>



If you are having a problem with end tags, could it be that you are
writing HTML code within script strings within an HTML document and that
it is somehow being interpreted by the browser? Try inserting an escaping
(backslash) character before the forward slash of all end tags occurring
in script strings.
 
M

mk834tt

(e-mail address removed) wrote in comp.lang.javascript:


Low hour xml/javascript pilot here. At
http://www.w3schools.com/xml/xml_to_html.asptrying to get an xml file
to render in ff2. The problem is table cells display "value </td>".
If I remove the end tag, "< /td>" from document.write (see lines 9 and
10) the text is clean and the end tag is placed properly. Does
document.write() have some smarts? I am assuming it does unless
someone can spot my error. Thank you.
Here's the code of interest:
01 for (var i = 0; i < cd.length; i++) {
02 var title = cd.getElementsByTagName("TITLE")
[0].childNodes[0].nodeValue;
03 var artist = cd.getElementsByTagName("ARTIST")
[0].childNodes[0].nodeValue;
04 var country = cd.getElementsByTagName("COUNTRY")
[0].childNodes[0].nodeValue;
05 var company = cd.getElementsByTagName("COMPANY")
[0].childNodes[0].nodeValue;
06 var price = cd.getElementsByTagName("PRICE")
[0].childNodes[0].nodeValue;
07 var year = cd.getElementsByTagName("YEAR")
[0].childNodes[0].nodeValue;
08 document.write("<tr>");
09 // document.write("<td>" + title + " < /td>");
10 document.write("<td>" + title);
11 document.write("<td>" + artist + " < /td>");
12 document.write("<td>" + country + " < /td>");
13 document.write("<td>" + company + " < /td>");
14 document.write("<td>" + price + " < /td>");
15 document.write("<td>" + year + " < /td>");
16 document.write("< /tr>");
17 }

Here's the first part of the xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>

If you are having a problem with end tags, could it be that you are
writing HTML code within script strings within an HTML document and that
it is somehow being interpreted by the browser? Try inserting an escaping
(backslash) character before the forward slash of all end tags occurring
in script strings.


Yes, the <script> is within the html body. See the link. \ and \\
and various permutations did not work. Thanks for a good suggestion.
I think I'll go the node route.
 
M

mk834tt

10 document.write("<td>" + title);

What about the end tag here ^^^^^^^^^

Jeff

That was the experiment. I replace 9 with 10. The text was properly
written when I removed the end tag. The cell text went from

Hide your heart < /td>
to
Hide your heart
 
R

RobG

It seems to "work" in Firefox for me, although it doesn't work in
Safari:

TypeError: Value undefined (result of expression xmlDoc.load) is not
object.
http://www.w3schools.com/xml/tryit_view.asp

 The problem is table
cells display "value </td>".  If I remove the end tag, "< /td>" from
document.write (see lines 9 and 10) the text is clean and the end tag
is placed properly.  Does document.write() have some smarts?  I am
assuming it does unless someone can spot my error.  Thank you.
Here's the code of interest:
01  for (var i = 0; i < cd.length; i++) {
02    var title =   cd.getElementsByTagName("TITLE")
[0].childNodes[0].nodeValue; [...]
07    var year =    cd.getElementsByTagName("YEAR")
[0].childNodes[0].nodeValue;
08    document.write("<tr>");
09    // document.write("<td>" + title + " < /td>");


  10    document.write("<td>" + title);

What about the end tag here            ^^^^^^^^^


Although the data file is XML, it seems it is being written to an HTML
document. In that case, the closing tag is optional.

However, it is never a good idea to write fragments of markup using
document.write, it is always much better to create a single string
with all the markup in it, then write it using a single document.write
statement.

Oh, and please trim quotes a little more... :)
 
T

Thomas 'PointedEars' Lahn

Low hour xml/javascript pilot here. At
http://www.w3schools.com/xml/xml_to_html.asp trying to get an xml file to
render in ff2.

Firefox 2 has a built-in non-validating XML parser, and you can use the XML
DOM there. No need to torture it with tag soup. Serve XHTML as
application/xhtml+xml to it instead.
The problem is table cells display "value </td>". If I remove the end
tag, "< /td>" from document.write (see lines 9 and 10) the text is clean
and the end tag is placed properly. Does document.write() have some
smarts? I am assuming it does unless someone can spot my error. Thank
you.

There is no end tag in either case. By (mindlessly) separating `<' and `/',
you have been destroying what the parser would have recognized as an
E(nd)TAGO(pen) delimiter (`</'). Since the end tag is optional, if you omit
your pseudo-end-tag, your markup is valid again. However, if this is HTML,
I strongly suggest you escape the ETAGO with `<\/' so that the `script'
element's CDATA does not end prematurely, or to outsource the `script'
element's content.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top