You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Jiang-Ackermann, Lijun" <lj...@debis.com> on 2000/06/07 17:58:40 UTC

Build-In Entities in Xerces Java Parser 1.0.3


Hi mailinglist,

I am writting a test program to read a XML Document by using Xerces Java
Parser 1.0.3.

In my XML document I used reference listed below instead of actual
characters:
----------------------------
&amp; for '&'
-----------------------------

so that my XML Document could for example be like:

--------------------------------------------
<Test>You &amp; me</Test>            
---------------------------------------------

Then I use following code to get the text value of Element <Test>:
----------------------------------------------------------------------------
--------------------
// Object node is the element <Test>
String text = node.getFirstChild().getNodeValue();
System.out.println("Value is " + text);
----------------------------------------------------------------------------
----------------------
"Value is You" instead "Value is You & me" is printed out.

Finally I have found out that XML Parser seperates the String "You &amp; me"
into 3 TextNodes.
1. The first node gets the value "You".
2. The second node gets the value "&";
3. The third node gets the value "me";

I should use following code.
----------------------------------------------------------------------------
-----------
       NodeList children = node.getChildNodes();
       String text = "";
       if ( children != null ) {
          int len = children.getLength();
          for ( int j = 0; j < len; j++ ) {
             Node child = children.item(j);
             text += child.getNodeValue();
          }
       }
      System.out.println("Value is " + text);

----------------------------------------------------------------------------
------------------
This time the result is correct: "Value is You & me"

I was also told that the Xerces Parser for c++ will take this String as only
one TextNode, so that the first solution could be used.

My question is, does anyone know, if this problem is a feature in Xerces
Java Parser or
it is a known bug.

Thanks very much for any advice.

Lijun




Re: Build-In Entities in Xerces Java Parser 1.0.3

Posted by Mike Pogue <mp...@apache.org>.
I believe the DOM spec allows a parser to do it either way.  You cannot assume
that a certain number of text nodes will be created for one block of text.

Mike

"Jiang-Ackermann, Lijun" wrote:
> 
> Hi mailinglist,
> 
> I am writting a test program to read a XML Document by using Xerces Java
> Parser 1.0.3.
> 
> In my XML document I used reference listed below instead of actual
> characters:
> ----------------------------
> &amp; for '&'
> -----------------------------
> 
> so that my XML Document could for example be like:
> 
> --------------------------------------------
> <Test>You &amp; me</Test>
> ---------------------------------------------
> 
> Then I use following code to get the text value of Element <Test>:
> ----------------------------------------------------------------------------
> --------------------
> // Object node is the element <Test>
> String text = node.getFirstChild().getNodeValue();
> System.out.println("Value is " + text);
> ----------------------------------------------------------------------------
> ----------------------
> "Value is You" instead "Value is You & me" is printed out.
> 
> Finally I have found out that XML Parser seperates the String "You &amp; me"
> into 3 TextNodes.
> 1. The first node gets the value "You".
> 2. The second node gets the value "&";
> 3. The third node gets the value "me";
> 
> I should use following code.
> ----------------------------------------------------------------------------
> -----------
>        NodeList children = node.getChildNodes();
>        String text = "";
>        if ( children != null ) {
>           int len = children.getLength();
>           for ( int j = 0; j < len; j++ ) {
>              Node child = children.item(j);
>              text += child.getNodeValue();
>           }
>        }
>       System.out.println("Value is " + text);
> 
> ----------------------------------------------------------------------------
> ------------------
> This time the result is correct: "Value is You & me"
> 
> I was also told that the Xerces Parser for c++ will take this String as only
> one TextNode, so that the first solution could be used.
> 
> My question is, does anyone know, if this problem is a feature in Xerces
> Java Parser or
> it is a known bug.
> 
> Thanks very much for any advice.
> 
> Lijun
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org