You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Lance Zhang <lz...@yahoo.com> on 2002/03/28 10:09:50 UTC

Problem in Dom parser

Hi,

I got a problem when using DOM parser in
javax.xml.parsers package.

I can not use getNodeValue() to get a text node value.
It return me some strange characters in doing that
although it is a very simple xml file.

Also, the node.getElementsByTagName always return me
an empty nodelist although it was obvious not null.

Does anyone have same kind of experience? does this
related to the encoding? 

thanks/Lance

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards�
http://movies.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Problem in Dom parser

Posted by Lance Zhang <lz...@yahoo.com>.
I tried iterating the whole dom tree. It gave me three
nodes. But the second nodes I got use nodelist.item(1)
is null. I tried several pasers, the result is the
same. Is it related to my environment? I am using VA
java. And I also imported the Xerces 2 into the
workspace.

thanks/lance
--- Hasan Sheboul <hs...@cs.nmsu.edu> wrote:
> Are you expecting to get the text in the element
> <b>?
> 
> This code is not accurate, since if you supply the
> same XML document without
> the corresponding DTD
> e.g. <!ELEMENT a (b)>
>  then the first child, that you expect to be <b>
> element, will  be a
> whitespace, which in turn is a #PCDATA, and not an
> element.
> 
> Try to iterate over the whole DOM tree, also have a
> look at the sample code
> examples that come with the xercesJ-2.
> 
> Lance Zhang wrote:
> 
> > OK,
> >
> > The code is like
> >
> > DocumentBuilderFactory
> > factory=DocumentBuilderFactory.newInstance();
> > DocumentBuilder
> builder=factory.newDocumentBuilder();
> > Document doc=builder.parse(new File("c:/a.xml");
> > String
> >
>
value=doc.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
> > //value come back something like ||
> >
> > a.xml
> > <?xml version="1.0" ?>
> > <a>
> >     <b>This is a test</b>
> > </a>
> >
> > thanks/lance
> >
> > --- Xuemin Guan <xu...@appresso.com> wrote:
> > > When posting such a question, it will be very
> > > helpful if the code snippet
> > > and xml sample
> > > file can be included.
> > >
> > > ----- Original Message -----
> > > �??信�??: "Lance Zhang" <lz...@yahoo.com>
> > > �?�?^ : <xe...@xml.apache.org>
> > > �??信�-��?, : 2002�?�??8�-?18:09
> > > 件�?? : Problem in Dom parser
> > >
> > >
> > > > Hi,
> > > >
> > > > I got a problem when using DOM parser in
> > > > javax.xml.parsers package.
> > > >
> > > > I can not use getNodeValue() to get a text
> node
> > > value.
> > > > It return me some strange characters in doing
> that
> > > > although it is a very simple xml file.
> > > >
> > > > Also, the node.getElementsByTagName always
> return
> > > me
> > > > an empty nodelist although it was obvious not
> > > null.
> > > >
> > > > Does anyone have same kind of experience? does
> > > this
> > > > related to the encoding?
> > > >
> > > > thanks/Lance
> > > >
> > > >
> __________________________________________________
> > > > Do You Yahoo!?
> > > > Yahoo! Movies - coverage of the 74th Academy
> > > Awards®
> > > > http://movies.yahoo.com/
> > > >
> > > >
> > >
> >
>
---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> > > xerces-j-user-unsubscribe@xml.apache.org
> > > > For additional commands, e-mail:
> > > xerces-j-user-help@xml.apache.org
> > > >
> > > >
> > >
> > >
> > >
> >
>
---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > > xerces-j-user-unsubscribe@xml.apache.org
> > > For additional commands, e-mail:
> > > xerces-j-user-help@xml.apache.org
> > >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Yahoo! Movies - coverage of the 74th Academy
> Awards�
> > http://movies.yahoo.com/
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards�
http://movies.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Problem in Dom parser

Posted by Hasan Sheboul <hs...@cs.nmsu.edu>.
Are you expecting to get the text in the element <b>?

This code is not accurate, since if you supply the same XML document without
the corresponding DTD
e.g. <!ELEMENT a (b)>
 then the first child, that you expect to be <b> element, will  be a
whitespace, which in turn is a #PCDATA, and not an element.

Try to iterate over the whole DOM tree, also have a look at the sample code
examples that come with the xercesJ-2.

Lance Zhang wrote:

> OK,
>
> The code is like
>
> DocumentBuilderFactory
> factory=DocumentBuilderFactory.newInstance();
> DocumentBuilder builder=factory.newDocumentBuilder();
> Document doc=builder.parse(new File("c:/a.xml");
> String
> value=doc.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
> //value come back something like ||
>
> a.xml
> <?xml version="1.0" ?>
> <a>
>     <b>This is a test</b>
> </a>
>
> thanks/lance
>
> --- Xuemin Guan <xu...@appresso.com> wrote:
> > When posting such a question, it will be very
> > helpful if the code snippet
> > and xml sample
> > file can be included.
> >
> > ----- Original Message -----
> > é??ä¿¡è??: "Lance Zhang" <lz...@yahoo.com>
> > å®?å?^ : <xe...@xml.apache.org>
> > é??ä¿¡æ-¥æ?, : 2002å¹?æ??8æ-?18:09
> > 件å?? : Problem in Dom parser
> >
> >
> > > Hi,
> > >
> > > I got a problem when using DOM parser in
> > > javax.xml.parsers package.
> > >
> > > I can not use getNodeValue() to get a text node
> > value.
> > > It return me some strange characters in doing that
> > > although it is a very simple xml file.
> > >
> > > Also, the node.getElementsByTagName always return
> > me
> > > an empty nodelist although it was obvious not
> > null.
> > >
> > > Does anyone have same kind of experience? does
> > this
> > > related to the encoding?
> > >
> > > thanks/Lance
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Yahoo! Movies - coverage of the 74th Academy
> > Awards®
> > > http://movies.yahoo.com/
> > >
> > >
> >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > xerces-j-user-unsubscribe@xml.apache.org
> > > For additional commands, e-mail:
> > xerces-j-user-help@xml.apache.org
> > >
> > >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail:
> > xerces-j-user-help@xml.apache.org
> >
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Movies - coverage of the 74th Academy Awards®
> http://movies.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Problem in Dom parser

Posted by Lance Zhang <lz...@yahoo.com>.
OK,

The code is like

DocumentBuilderFactory
factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(new File("c:/a.xml");
String
value=doc.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
//value come back something like ||

a.xml
<?xml version="1.0" ?>
<a>
    <b>This is a test</b>
</a>  

thanks/lance


--- Xuemin Guan <xu...@appresso.com> wrote:
> When posting such a question, it will be very
> helpful if the code snippet
> and xml sample
> file can be included.
> 
> ----- Original Message -----
> 送信�?: "Lance Zhang" <lz...@yahoo.com>
> 宛先 : <xe...@xml.apache.org>
> 送信日時 : 2002�?�?8�?18:09
> 件名 : Problem in Dom parser
> 
> 
> > Hi,
> >
> > I got a problem when using DOM parser in
> > javax.xml.parsers package.
> >
> > I can not use getNodeValue() to get a text node
> value.
> > It return me some strange characters in doing that
> > although it is a very simple xml file.
> >
> > Also, the node.getElementsByTagName always return
> me
> > an empty nodelist although it was obvious not
> null.
> >
> > Does anyone have same kind of experience? does
> this
> > related to the encoding?
> >
> > thanks/Lance
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Yahoo! Movies - coverage of the 74th Academy
> Awards®
> > http://movies.yahoo.com/
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> >
> >
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards�
http://movies.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Problem in Dom parser

Posted by Xuemin Guan <xu...@appresso.com>.
When posting such a question, it will be very helpful if the code snippet
and xml sample
file can be included.

----- Original Message -----
送信者 : "Lance Zhang" <lz...@yahoo.com>
宛先 : <xe...@xml.apache.org>
送信日時 : 2002年3月28日 18:09
件名 : Problem in Dom parser


> Hi,
>
> I got a problem when using DOM parser in
> javax.xml.parsers package.
>
> I can not use getNodeValue() to get a text node value.
> It return me some strange characters in doing that
> although it is a very simple xml file.
>
> Also, the node.getElementsByTagName always return me
> an empty nodelist although it was obvious not null.
>
> Does anyone have same kind of experience? does this
> related to the encoding?
>
> thanks/Lance
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Movies - coverage of the 74th Academy Awards®
> http://movies.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org