/    Sign up×
Community /Pin to ProfileBookmark

getElementsByTagName problem when the element is empty.

Not sure why that happens but when I parse the XML I get as a response to the AJAX post, it sometimes displays an error (null object) if the element exists but empty ?

The XML I get contains some records that have optional fields. Any ideas?

Thanks.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 10.2009 —  Any ideas?
[/QUOTE]

Any code? Or any link to a test page? How could we possible know what happens without seeing the code?
Copy linkTweet thisAlerts:
@mesh2005authorAug 10.2009 — Hi,

Sorry, I didn't add my code to the post. Here is a sample of the XML and the JS code:
[CODE]<root>
[INDENT]<item>
[INDENT][INDENT]<date>2009-05-10</date>
<title />[/INDENT][/INDENT]
</item>[/INDENT]
</root>

1. var response = xmlHttp.responseXML.documentElement;
2. var items = response.getElementsByTagName('item');
3. var title = items[0].getElementsByTagName('title')[0].firstChild.data;
4. var date = items[0].getElementsByTagName('date')[0].firstChild.data;[/CODE]


The above XML causes an error (null object) at statement #3. It works fine if the title element has a value (e.g. <title>Test</title>).

Thanks.
Copy linkTweet thisAlerts:
@KorAug 12.2009 — [B]<title />[/B] ??

That is not a valid XML syntax... That is a sort of XHTML like-syntax, which is something else. What is that [B]<title />[/B] for? At least make it [B]<title></title>[/B]
Copy linkTweet thisAlerts:
@Jeff_MottAug 12.2009 — [B]<title />[/B] ??

That is not a valid XML syntax... [/QUOTE]


Actually, it is.

mesh2005, I think the problem comes when you use ".firstChild.data" on the title node. Notice that the title node is empty. It has no child nodes, so firstChild will be null. Which means when you use ".data", you're using it on a null value. Probably you'll need to start using some conditional expressions.

var titleNode = items[0].getElementsByTagName('title')[0];
var title = titleNode.firstChild ? titleNode.firstChild.data : "";
Copy linkTweet thisAlerts:
@KorAug 13.2009 — Actually, it is.

[/QUOTE]

Actually I don't think it is. Never saw that specification into trhe XML W3C recs.I might be wrong, but can you give me a counter-example?
Copy linkTweet thisAlerts:
@Jeff_MottAug 13.2009 — http://www.w3.org/TR/xml/#d0e2480

The reason this syntax seems "XHTML-like" is because XHTML is an XML application, so XHTML inherits (for lack of a better word) its syntax from XML.
Copy linkTweet thisAlerts:
@KorAug 14.2009 — There's exactly the place where it is written: "the empty-element tag SHOULD be used, and SHOULD [COLOR="Blue"]only[/COLOR] be used, for elements which are declared EMPTY." From this line I always understood that empty-element tags are [I]XHTML only[/I], because I see no reason for using EMPTY elements in XML documents. I always took empty-element tags as XML extensions towards XHTML, not an XML standalone syntax.

This is easy to be seen whenever you want do declare an empty element using an XML schema. In the end the code will avoid the < /> notation:

<i>
</i>&lt;xsd:element name="img"&gt;
.
.
.
&lt;/xsd:element&gt;


Well yes, from a direct point of view you are right: an empty element is XML valid. But that might not make the XML document well formatted as well.

Anyway, let's hope [B]mesh2005[/B] has solved his problem, a way or another ?
Copy linkTweet thisAlerts:
@Jeff_MottAug 14.2009 — But that might not make the XML document well formatted as well.[/quote]Empy-element tags do not affect either well-formdness or validity.

I always understood that empty-element tags are XHTML only, because I see no reason for using EMPTY elements in XML documents.[/quote]You'll find empty elements in RSS, Atom and XSL documents, to name a few. They're actually quite common.

I always took empty-element tags as XML extensions towards XHTML, not an XML standalone syntax.[/quote]Well... that's just not the case.
×

Success!

Help @mesh2005 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...