Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Java - saxparser ignore <!doctype> line

 
Thread Tools Search this Thread
Old 01-05-2005, 03:30 PM   #1
SnooPac
 
Posts: n/a
Default saxparser ignore <!doctype> line

Hi.

I'm trying to use SAXParser simply to extract some attribute values from an
XML file. No validations or anything.
So I have it all set up and working....mostly.

The problem is that the XML file (which can't be changed) has the line at
the start that says:
<!DOCTYPE BlaCfg SYSTEM "../../../path/to/dtd/file/BlaCfg.dtd">

And this dtd cannot be found.

There are some workarounds I can do (like regenerating this XML file
temporarily without this line, or making a dummy dtd file, or...?), but I'd
rather just have my SAXParser ignore this line entirely. Is there any
property I can set to do this?

Here is some of my code, by the way...I think its very standard:

DefaultHandler handler = new MyTempXmlParser();
SAXParserFactory factory = SAXParserFactory.newInstance();

// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File("path/to/xml/file/file.xml"), handler);

I was hoping maybe for some sort of factory.setFeature() line that might do
this?
Or maybe within my handler?

Any hints would be appreciated.

Thanks,
Aiman


  Reply With Quote
Old 01-05-2005, 04:23 PM   #2
Stanimir Stamenkov
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

/SnooPac/:

> DefaultHandler handler = new MyTempXmlParser();
> SAXParserFactory factory = SAXParserFactory.newInstance();


Try setting, explicitly:

factory.setValidating(false);

It should be equivalent to setting the
"http://xml.org/sax/features/validation" feature to false.

> // Parse the input
> SAXParser saxParser = factory.newSAXParser();
> saxParser.parse( new File("path/to/xml/file/file.xml"), handler);
>
> I was hoping maybe for some sort of factory.setFeature() line that might do
> this?


http://www.saxproject.org/apidoc/org...ge_description

--
Stanimir
  Reply With Quote
Old 01-05-2005, 06:49 PM   #3
SnooPac
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

> Try setting, explicitly:
>
> factory.setValidating(false);


Although i havent done this exactly, I did check factory.isValidating() and
it was already set to false.

>

http://www.saxproject.org/apidoc/org...ge_description

Thanks for the list.
From that page, I tried factory.setFeature("resolve-dtd-uris", false);
but from that i got the exception: org.xml.sax.SAXNotRecognizedException:
Feature: resolve-dtd-uris

So it doesnt look like my jdk's (sun 1.4.2 i believe) matches whats in that
webpage you sent
Anyway, I dont really know if (or think that) setting "resolve-dtd-uris" to
false would have helped anyway, but it looked like the closest thing to what
I wanted.

Thanks though.


  Reply With Quote
Old 01-05-2005, 06:54 PM   #4
Paweł Stobiński
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

SnooPac <> wypluł(a):
> I was hoping maybe for some sort of factory.setFeature() line that might
> do this?
> Or maybe within my handler?



Hi,
I thing you rather need to set your own EntityResolver:


final SAXBuilder parser = new SAXBuilder();
parser.setEntityResolver(new EntityResolver()
{
public InputSource resolveEntity(String publicId, String systemId)
{
if (/*publicId is not right*/)
return new InputSource(
new ByteArrayInputStream("<?xml version='1.0'
encoding='UTF-8'?>".getBytes()));
else
return null;
}
});


BR.
--
Paweł Stobiński // SQ9NRY/5 // GG 0x4F9228
"Windows [n.], A 32-bit extension and GUI shell to a 16-bit patch to
an 8-bit operating system originally coded for a 4-bit microprocessor
and produced by a 2-bit company without 1 bit of sense

  Reply With Quote
Old 01-05-2005, 06:56 PM   #5
Paweł Stobiński
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

SnooPac <> wypluł(a):
> I was hoping maybe for some sort of factory.setFeature() line that might
> do this?
> Or maybe within my handler?


Hi,
I think you rather need to set your own EntityResolver:


final SAXBuilder parser = new SAXBuilder();
parser.setEntityResolver(new EntityResolver()
{
public InputSource resolveEntity(String publicId, String systemId)
{
if (/publicId is not right/)
return new InputSource(
new ByteArrayInputStream("<?xml version='1.0'
encoding='UTF-8'?>".getBytes()));
else
return null;
}
});


BR.
--
Paweł Stobiński // SQ9NRY/5 // GG 0x4F9228
"Windows [n.], A 32-bit extension and GUI shell to a 16-bit patch to
an 8-bit operating system originally coded for a 4-bit microprocessor
and produced by a 2-bit company without 1 bit of sense

  Reply With Quote
Old 01-05-2005, 08:14 PM   #6
SnooPac
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

Ok, I used a variation of this, and it seems to be working.
Although I don't really understand it, so it seems rather cludgy.

I didn't use saxbuilder or anything. I just overrode the resolveEntity()
function from DefaultHandler (in my handler class).
The other problem I had was that my publicId was being reported as null, and
systemId was reported as file://path/to/myfile.dtd.
So I cant really do anything in my if statement concerning publicId.
so now I have my resolveEntity() returning "<?xml ..." regardless. I guess I
could do some kind of processing on systemId (like check if it starts with
file:// and ends with .dtd, or something else that's simple).

Another thing I don't understand, is what exactly happens when I return that
<?xml... stuff? Will the parser now continue parsing the document using the
InputSource thats returned instead of the <doctype> line from the xml file?

Thanks again,
Aiman

"Paweł Stobiński" <> wrote in message
news:crhgne$due$...
> SnooPac <> wypluł(a):
> > I was hoping maybe for some sort of factory.setFeature() line that might
> > do this?
> > Or maybe within my handler?

>
> Hi,
> I think you rather need to set your own EntityResolver:
>
>
> final SAXBuilder parser = new SAXBuilder();
> parser.setEntityResolver(new EntityResolver()
> {
> public InputSource resolveEntity(String publicId, String systemId)
> {
> if (/publicId is not right/)
> return new InputSource(
> new ByteArrayInputStream("<?xml version='1.0'
> encoding='UTF-8'?>".getBytes()));
> else
> return null;
> }
> });
>
>
> BR.
> --
> Paweł Stobiński // SQ9NRY/5 // GG 0x4F9228
> "Windows [n.], A 32-bit extension and GUI shell to a 16-bit patch to
> an 8-bit operating system originally coded for a 4-bit microprocessor
> and produced by a 2-bit company without 1 bit of sense
>



  Reply With Quote
Old 01-05-2005, 08:31 PM   #7
Paweł Stobiński
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

SnooPac <> wypluł(a):
> Another thing I don't understand, is what exactly happens when I return
> that <?xml... stuff? Will the parser now continue parsing the document
> using the InputSource thats returned instead of the <doctype> line from
> the xml file?


No, in fact that <?xml... return string served as example. In the case it
should rather be <!DOCTYPE.., or any transformation of input data to avoid
referring to any unknown locations.

BR.
--
Paweł Stobiński // SQ9NRY/5 // GG 0x4F9228
"Windows [n.], A 32-bit extension and GUI shell to a 16-bit patch to
an 8-bit operating system originally coded for a 4-bit microprocessor
and produced by a 2-bit company without 1 bit of sense

  Reply With Quote
Old 01-06-2005, 02:07 PM   #8
Stanimir Stamenkov
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

/SnooPac/:

>> Try setting, explicitly:
>>
>> factory.setValidating(false);

>
> Although i havent done this exactly, I did check factory.isValidating() and
> it was already set to false.
>
> http://www.saxproject.org/apidoc/org...ge_description


One could notice that on the above page the "validation" feature has
unspecified default value so one probably need calling
'setValidating(false)' anyway although the Java API describes it is
the default. You may try calling even:

factory.setFeature("http://xml.org/sax/features/validation", false);

You didn't say: did you actually try setting it explicitly? You
stated you only checked 'getValidating()', but could be misleading
sometimes.

> Thanks for the list.
> From that page, I tried factory.setFeature("resolve-dtd-uris", false);
> but from that i got the exception: org.xml.sax.SAXNotRecognizedException:
> Feature: resolve-dtd-uris
>
> So it doesnt look like my jdk's (sun 1.4.2 i believe) matches whats in that
> webpage you sent
> Anyway, I dont really know if (or think that) setting "resolve-dtd-uris" to
> false would have helped anyway, but it looked like the closest thing to what
> I wanted.


I don't think this feature is any use for you.

--
Stanimir
  Reply With Quote
Old 01-06-2005, 02:16 PM   #9
Stanimir Stamenkov
 
Posts: n/a
Default Re: saxparser ignore <!doctype> line

/SnooPac/:

>> factory.setValidating(false);

>
> Although i havent done this exactly, I did check factory.isValidating() and
> it was already set to false.


From my experience the default behavior of parser implementations
is to validate when a DOCTYPE declaration has been encountered and
not to validate if no DOCTYPE has been specified, regardless of what
the initial JAXP factory.getValidation() returns. So you should set
factory.setValidation(true/false) explicitly if you want to trigger
exact behavior.

--
Stanimir
  Reply With Quote
Reply

« final class | jpg »

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
SONY DVD RW DW-G120A SOMETIMES FAILS...... atlantic965 DVD Video 0 06-18-2006 09:36 PM
DVD Verdict reviews: WALK THE LINE, JARHEAD, WALK THE LINE: TWO-DISC COLLECTOR'S EDITION, and more! DVD Verdict DVD Video 0 03-20-2006 08:19 AM
problems backing up dvds Lawrence Traub DVD Video 11 09-27-2005 06:34 PM
Burn process failed - help! Log file posted for help troubleshooting Michael Mason DVD Video 1 08-16-2004 08:24 PM
Pioneer A05 Problems Bill Stock DVD Video 8 11-28-2003 04:03 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47