Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > DocumentBuilder's parse method is not able to read a tag such as this one <weight/>

Reply
Thread Tools

DocumentBuilder's parse method is not able to read a tag such as this one <weight/>

 
 
BeGreen
Guest
Posts: n/a
 
      08-13-2006
Hi All,

javax.xml.parsers.DocumentBuilder's parse method is not able to parse
the xml file, when
the record has an empty tag without a value, such as <weight/>.

Any Java class, you may know which can parse that tag successfully or
even ignore it?
Thanks!

see below a sample of my xml file:



<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
<Record>
<Record>
<PartNo>02</PartNo>


<weight>A</weight>
</Record>
</root>

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-13-2006
BeGreen wrote:
> javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> the xml file, when
> the record has an empty tag without a value, such as <weight/>.
>
> Any Java class, you may know which can parse that tag successfully or
> even ignore it?
> Thanks!
>
> see below a sample of my xml file:
>
>
>
> <?xml version="1.0" encoding="ISO-8859-1" ?>
> <root>
> <Record>
> <PartNo>01</PartNo>
> <weight/>
> <Record>
> <Record>
> <PartNo>02</PartNo>
>
>
> <weight>A</weight>
> </Record>
> </root>


Java parses that XML just fine.

Assuming you change the bad <Record> to </Record> so
the XML becomes wellformed !

Arne
 
Reply With Quote
 
 
 
 
BeGreen
Guest
Posts: n/a
 
      08-13-2006
Arne, sorry for that. Actually I forgot the the forward slash on the
record, it's:

<Record>
<PartNo>01</PartNo>
<weight/>
</Record>



My only problem is to parse an empty tag with this format <weight/>




<Record> t</Record>
Arne Vajh°j wrote:
> BeGreen wrote:
> > javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> > the xml file, when
> > the record has an empty tag without a value, such as <weight/>.
> >
> > Any Java class, you may know which can parse that tag successfully or
> > even ignore it?
> > Thanks!
> >
> > see below a sample of my xml file:
> >
> >
> >
> > <?xml version="1.0" encoding="ISO-8859-1" ?>
> > <root>
> > <Record>
> > <PartNo>01</PartNo>
> > <weight/>
> > <Record>
> > <Record>
> > <PartNo>02</PartNo>
> >
> >
> > <weight>A</weight>
> > </Record>
> > </root>

>
> Java parses that XML just fine.
>
> Assuming you change the bad <Record> to </Record> so
> the XML becomes wellformed !
>
> Arne


 
Reply With Quote
 
BeGreen
Guest
Posts: n/a
 
      08-13-2006
Arne, sorry for that. Actually I forgot the forward slash on the
record, it's:
.................................................. .............................
<Record>
<PartNo>01</PartNo>
<weight/>
</Record>
.................................................. ............................


My only problem is to parse an empty tag with this format <weight/>



> Arne Vajh°j wrote:
> > BeGreen wrote:
> > > javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> > > the xml file, when
> > > the record has an empty tag without a value, such as <weight/>.
> > >
> > > Any Java class, you may know which can parse that tag successfully or
> > > even ignore it?
> > > Thanks!
> > >
> > > see below a sample of my xml file:
> > >
> > >
> > >
> > > <?xml version="1.0" encoding="ISO-8859-1" ?>
> > > <root>
> > > <Record>
> > > <PartNo>01</PartNo>
> > > <weight/>
> > > <Record>
> > > <Record>
> > > <PartNo>02</PartNo>
> > >
> > >
> > > <weight>A</weight>
> > > </Record>
> > > </root>

> >
> > Java parses that XML just fine.
> >
> > Assuming you change the bad <Record> to </Record> so
> > the XML becomes wellformed !
> >
> > Arne


 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      08-13-2006
BeGreen schrieb:
> Hi All,
>
> javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> the xml file, when
> the record has an empty tag without a value, such as <weight/>.
>
> Any Java class, you may know which can parse that tag successfully or
> even ignore it?
> Thanks!


import java.io.*;
import javax.xml.parsers.*;

public class XMLTest {
public static final void main( String args[] ) throws Exception {
String xml =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
"<root>\n" +
" <Record>\n" +
" <PartNo>01</PartNo>\n" +
" <weight/>\n" +
" </Record>\n" +
" <Record>\n" +
" <PartNo>2</PartNo>\n" +
" <weight>A</weight>\n" +
" </Record>\n" +
"</root>";

System.out.println( xml );
byte data[] = xml.getBytes("ISO-8859-1");
ByteArrayInputStream is = new ByteArrayInputStream( data );

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(is);
is.close();
System.out.println("Done.");
}
}

Bye
Michael
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      08-13-2006
Michael Rauscher wrote:
> BeGreen schrieb:

....
> > javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> > the xml file, when
> > the record has an empty tag without a value, such as <weight/>.

....
> import java.io.*;
> import javax.xml.parsers.*;

.....
> System.out.println("Done.");
> }
> }


That was a lovely little ..example.

Andrew T.

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-13-2006
BeGreen wrote:
> My only problem is to parse an empty tag with this format <weight/>


That is not a problem.

Java XML parsers do not have a problem with that.

Arne
 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      08-13-2006
Andrew Thompson schrieb:
>> import java.io.*;
>> import javax.xml.parsers.*;

> ....
>> System.out.println("Done.");
>> }
>> }

>
> That was a lovely little ..example.


Please provide a sscce

Bye
Michael
 
Reply With Quote
 
BeGreen
Guest
Posts: n/a
 
      08-13-2006

Hi Michael,

Yes, I can be able to parse the xml data, but my program is failing
when executing the code below, to retrieve the Record where there is
this empty tag without a value, <weight/>

<weight>something</weight> can be retrieved, but why not this one
<weight/>

As far as I am concerned <weight/> is a well-formed empty tag, and it
should be retrieved successfully!

There must an XML/Jave API to be able to retrieve it!


.................................................. .............................................

if (node.getNodeName()=="weight") {
String weightvalue = node.getFirstChild().getNodeValue();
}

-----------------------------------------------------------------------------------------------

For your info, Node is the object of the class: org.w3c.dom.Node





see below a sample of my xml file:


<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
</Record>
<Record>
<PartNo>02</PartNo>


<weight>A</weight>
</Record>
</root>

 
Reply With Quote
 
BeGreen
Guest
Posts: n/a
 
      08-13-2006
Arne,

See below my respone to Michael.

Yes, I can be able to parse the xml data, but to retrieve this kind of
empty tag <weight/> is my problem.

<weight> something </weight> can be retrieved though!

Thanks!


Arne Vajh°j wrote:
> BeGreen wrote:
> > My only problem is to parse an empty tag with this format <weight/>

>
> That is not a problem.
>
> Java XML parsers do not have a problem with that.
>
> Arne


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to display color in excel not able to display color in excel using xml sed_y XML 0 02-15-2012 09:46 PM
how to read parse data from two file to generate one one file Asif Iqbal Ruby 0 08-06-2009 04:47 PM
DocumentBuilder object is not able to parse a XML String which has a nodename which contains forward slash! Ed Java 6 08-02-2007 03:29 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
not able to parse this xml , i wana use xml::simple preferably HP Perl Misc 9 01-02-2006 06:52 PM



Advertisments