Hi!

This is a standard error everybody that starts to use DOM make.
The resulting dom tree from your document does actually look like this

root
  #text
  tag1
    #text
    tag11
      #text (11 text)
    #text
    tag12
      #text (12 text)
    #text
  #text
  tag2
    #text
    tag21
      #text (21 text)
    #text
    tag22
      #text (21 text)
    #text
  #text

All newlines and spaces in the document are kept in text nodes if you not
explicitly tell the parser to ignore them. To be able to ignore whitespace
then you need to set a flag in the parser and validate against a DTD that
specifies the tags as having only element content like this.

<!ELEMENT tag1 (tag11, tag12)>

The default element (which is used if you are not validating) is defined
like this...

<!ELEMENT tag1 #PCDATA>

..which tells the parser to keep everything including all whitespace.
Another way to go is to pack your data in a less readable form by removing
all whitepaces like this.

<root><tag1><tag11>11 text</tag11><tag12>12
text</tag12></tag1><tag2><tag21>21 text</tag21><tag22>22
text</tag22></tag2></root>

Then you will have no 'unneccesary' text nodes.

Hope my ranting made things clearer for you.
Happy hackin' :)

Erik Rydgren
Mandarinen AB
Sweden

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Glenn Lewis
Sent: den 15 oktober 2001 08:47
To: [EMAIL PROTECTED]
Subject: getNodeValue question


Hi.

I am new to XML and xerces. I am hoping that someone can explain to me
why code segment 1 prints #text and then seg faults, but code segment 2
(also below) prints the lines:
  tag2
  21 text
as expected?

The xml file is:

<root>
  <tag1>
    <tag11>11 text</tag11>
    <tag12>12 text</tag12>
  </tag1>

  <tag2>
    <tag21>21 text</tag21>
    <tag22>22 text</tag22>
  </tag2>
</root>


Code segment 1
---------------
    DOM_Document doc = parser.getDocument();
    DOM_Element rootElement = doc.getDocumentElement();
    DOMString tag2Name = DOMString("tag2");
    DOM_NodeList nodeList = rootElement.getElementsByTagName(tag2Name);
    DOM_Node node2 = nodeList.item(0);
    DOM_Node node21 = node2.getFirstChild();
    node21.getNodeName().println();
    node21.getFirstChild().getNodeValue().println();

Code segment 2
---------------
    DOM_Document doc = parser.getDocument();
    DOM_Element rootElement = doc.getDocumentElement();
    DOMString tag21Name = DOMString("tag21");
    DOM_NodeList nodeList = rootElement.getElementsByTagName(tag21Name);
    DOM_Node node21 = nodeList.item(0);
    node21.getNodeName().println();
    node21.getFirstChild().getNodeValue().println();


Thanks! (sorry if the answer is obvious!)

--
Glenn

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to