473,385 Members | 1,893 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,385 software developers and data experts.

How to check if an element exist reading XML...

<record>
<title>Lunar Park</title>
<author>Bret Easton Ellis</author>
<publisher></publisher>
<year>2005</year>
</record>

xAuthor =
x[i].getElementsByTagName("author")[0].firstChild.nodeValue
xPublisher =
x[i].getElementsByTagName("publisher")[0].firstChild.nodeValue

I'm retrieving data from a xml file, and I need to display that data in
a table, but as soon as I try to reference an element that it's empty
the browser stops working...

Is there any way to prevent this (so, to know wich elements are empty
in a certain "record")?

Thanks a lot in advance.

Nov 23 '05 #1
2 5468
ne****@googlemail.com wrote:
<record>
<title>Lunar Park</title>
<author>Bret Easton Ellis</author>
<publisher></publisher>
<year>2005</year>
</record>

xAuthor =
x[i].getElementsByTagName("author")[0].firstChild.nodeValue
xPublisher =
x[i].getElementsByTagName("publisher")[0].firstChild.nodeValue

I'm retrieving data from a xml file, and I need to display that data in
a table, but as soon as I try to reference an element that it's empty
the browser stops working...
Yes, because if there is no firstChild, attempting to get its nodeValue
will cause an error. You could try:

var t = x[i].getElementsByTagName("author");
xAuthor = (t[0] && t[0].firstChild)? t[0].firstChild.nodeValue : '';


Is there any way to prevent this (so, to know wich elements are empty
in a certain "record")?


A more robust solution is to write a getValue() function:

xAuthor = getValue(x[i].getElementsByTagName("author"));
and getValue() can be something like:

function getValue(C){
var val='';
for (var i=0, len=C.length; i<len; ++i){
val += C[i].nodeValue;
}
return val;
}

You may want to add some smarts to getValue() to manage different
element types.

--
Rob
Nov 23 '05 #2
On 2005-11-12, ne****@googlemail.com <ne****@googlemail.com> wrote:
<record>
<title>Lunar Park</title>
<author>Bret Easton Ellis</author>
<publisher></publisher>
<year>2005</year>
</record>

xAuthor =
x[i].getElementsByTagName("author")[0].firstChild.nodeValue
xPublisher =
x[i].getElementsByTagName("publisher")[0].firstChild.nodeValue

I'm retrieving data from a xml file, and I need to display that data in
a table, but as soon as I try to reference an element that it's empty
the browser stops working...

Is there any way to prevent this (so, to know wich elements are empty
in a certain "record")?


somethinng like

x[i].getElementsByTagName("publisher")[0].children.length

I think.

Bye.
Jasen
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
by: Y.S. | last post by:
Hello All, I have the following schema: .... <xs:element name="data_type_id" type="xs:unsignedShort" /> <xs:element name="data_value" type="xs:anyType" /> .... What I want to do is to...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
23
by: Randell D. | last post by:
Folks, I have written some scripting tools that are compatable with alot of my pages - For example, I've created a script that will check to ensure certain form fields that require data, are...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
2
by: awoodgate | last post by:
Hi, I'm trying to read the width of a css absolutely positioned element in javascript. If I use an id selector or a class definition then the width is null, but if I use an inline style then it...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
9
by: Chris Ashley | last post by:
How do I check if an object exists in Javascript? EG: if (document.getElementById("product").value == null) { prodValue = ""; } else {
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...
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,...

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.