![]() |
|
|
|||||||
![]() |
Java - saxparser ignore <!doctype> line |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Posts: n/a
|
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 |
|
|
|
#2 |
|
Posts: n/a
|
/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 |
|
|
|
#3 |
|
Posts: n/a
|
> 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. |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 > |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
/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 |
|
|
|
#9 |
|
Posts: n/a
|
/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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |