DOM

C

Cathy

Hi,

I am new to XML. I have a questions regarding DOM.

<root>
<demo demoatt="testattr">This is PCDATA</domo>
</root>

The book I read presents 2 ways to get This is PCDATA
(1) selectsinglenode("/root/demo").nodevalue
(2) selectsinglenode("/root/demo[0]").firstchild.nodevalue

Why do I need to use firstchild in the 2nd method? Thanks.
 
M

Martin Honnen

Cathy wrote:

I have a questions regarding DOM.

<root>
<demo demoatt="testattr">This is PCDATA</domo>
</root>

The book I read presents 2 ways to get This is PCDATA
(1) selectsinglenode("/root/demo").nodevalue
(2) selectsinglenode("/root/demo[0]").firstchild.nodevalue

Why do I need to use firstchild in the 2nd method? Thanks.

What you have above is XPath mixed with DOM, the XPath
/root/demo
selects <demo> element nodes that are children of the <root> element.
The nodeValue of an element node is always null thus it doesn't help to
access selectsinglenode("/root/demo").nodevalue as that yields null. I
would suggest to use
selectSingleNode("/root/demo/text()").nodevalue
as that way the text node that is a child of the <demo> element node is
selected and that is the node you are looking for.
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top