Quite easy just call getDocument method on the DOMParser object.
e.g.
import java.io.*; import org.apache.xerces.parsers.*; import org.xml.sax.SAXException; import org.w3c.dom.*;
public class DomTestParser {
public static void main(String args[]) {DOMParser parser = new DOMParser();
try {System.out.println("file = " + args[0]);
parser.setFeature("http://apache.org/xml/features/standard-uri-conformant",false);
parser.parse( args[0] );
Document doc = parser.getDocument();
} catch (SAXException ex) {
ex.printStackTrace();
System.err.println("could not set parser feature");
} catch (IOException ex ){
ex.printStackTrace();
}} }
I think that DOMParser has already set the feature as false by default.
The next problem could be that if you may get a server connection exception.
Java is pretty fuzzy about using UNC paths. :<
Regards,
From: "Jianlin Chang" <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: <[EMAIL PROTECTED]>, "Jeffrey Rodriguez " <[EMAIL PROTECTED]>
Subject: RE: DOM parser problem with handling Windows UNC path for XML and DTD file
Date: Wed, 10 Sep 2003 13:46:30 -0400
Thanks.
As you can see from my example, I am using JAXP API. Setting the parer feature using the way you desribed doesn't seem to affect the DocumentBuilder.parse() method. From the DOMParser, how do I get a 'Document' that my other part of code use?
-----Original Message----- From: Jeffrey Rodriguez [mailto:[EMAIL PROTECTED] Sent: September 10, 2003 1:38 PM To: [EMAIL PROTECTED] Subject: Re: DOM parser problem with handling Windows UNC path for XML and DTD file
Janlin,
Most answers to your question are in the FAQ and documentation.
You need to set a parser property to being able to parse UNC.
set the following property to false.
http://apache.org/xml/features/standard-uri-conformant
The DocumentBuilderFactory interface contains a setAttribute(String,Object) method which may provide a means to set features and properties on the underyling parser. However, it cannot be relied upon. Therefore, you must use the Xerces DOMParser object directly. For example:
import org.apache.xerces.parsers.DOMParser; import org.xml.sax.SAXException;
DOMParser parser = new DOMParser();
try {
parser.setFeature(http://apache.org/xml/features/standard-uri-conformant, false);
} catch (SAXException e) { System.err.println("could not set parser feature"); }
>
>It looks that there are problems with handling Windows UNC path (a path in
>the format of \\computer\share\filename). I would like to seek help on how
>to solve the problem.
>
Set the feature to false to process non conformant URI.
>For the DocumentBuilder.parse() method, if a UNC path name is specified as
>the parameter, it will fail will error 'java.net.MalformedURLException: no
>protocol'.
Of course because it is not standard.
>I tried two approaches. 1. set validating to false, but it looks that it
>still try to find the DTD file. 2. Using EntityResolver to resolve the DTD
>file, but it looks that the EntityResolver is never called (the print
>statement doesn't print anything). Why is this not called?
>
+ No setting validation to false won't work, because the parser needs to get
the DTD even
when it does not validate to resolve entities.
+Entity Resolver gets called after finding non standard URI, by then you already have a MalformedURL Exception.
Try what I said at beggining of note of setting parser feature.
Another possibility is to write a method that converts your non-conformant
URI into a conformant one.
Notice that in your program the MalformedURL exception if probably thrown by
Java when you
pass the path to URI class.
Regards,
Jeffrey Rodriguez Silicon Valley
>From: "Jianlin Chang" <[EMAIL PROTECTED]>
>Reply-To: [EMAIL PROTECTED]
>To: <[email protected]>, <[EMAIL PROTECTED]>
>Subject: DOM parser problem with handling Windows UNC path for XML and DTD
>file
>Date: Wed, 10 Sep 2003 12:31:36 -0400
>
>It looks that there are problems with handling Windows UNC path (a path in
>the format of \\computer\share\filename). I would like to seek help on how
>to solve the problem.
>
>For the DocumentBuilder.parse() method, if a UNC path name is specified as
>the parameter, it will fail will error 'java.net.MalformedURLException: no
>protocol'.
>I have to use 'DocumentBuilder.parse(new File(uncPath))'. Then it got
>problems with find DTD file that the XML file uses:
><!DOCTYPE Users SYSTEM "Users.dtd">
>The error is:
>'Connection refused: connect'. From the stack trace, it looks that it trys
>to find the DTD file using FTP.
>
>I tried two approaches. 1. set validating to false, but it looks that it
>still try to find the DTD file. 2. Using EntityResolver to resolve the DTD
>file, but it looks that the EntityResolver is never called (the print
>statement doesn't print anything). Why is this not called?
>
>How can I solve the problem?
>
>The sample code is enclosed.
>
>xerces-j version I used is 2.0.2.
>
>Thanks.
>
>
>import javax.xml.parsers.*;
>import org.w3c.dom.*;
>import org.xml.sax.*;
>import org.apache.xml.serialize.OutputFormat;
>import org.apache.xml.serialize.Serializer;
>import org.apache.xml.serialize.SerializerFactory;
>import org.apache.xml.serialize.XMLSerializer;
>
>public class TestDOM {
> static public void main(String args[])
> {
> try {
> DocumentBuilderFactory docBuilderFactory =
>DocumentBuilderFactory.newInstance();
> docBuilderFactory.setValidating(false);
> DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
> docBuilder.setEntityResolver(new MyEntityResolver());
>// Document m_domDocument = docBuilder.parse(args[0]);
> Document m_domDocument = docBuilder.parse(new java.io.File(args[0]));
> } catch (ParserConfigurationException exn) {
> System.err.println("Got ParserConfigurationException: " +
>exn.getMessage());
> } catch (SAXException exn) {
> System.err.println("Got SAXException: " + exn.getMessage());
> } catch (java.io.IOException exn) {
> System.err.println("Got IOException: " + exn.getMessage());
> exn.printStackTrace();
> }
> }
>}
>
>class MyEntityResolver implements EntityResolver {
> public InputSource resolveEntity(String publicId, String systemId)
> throws SAXException, java.io.IOException
> {
> System.out.println("MyEntityResolver: " + systemId);
> java.io.FileInputStream fs = new java.io.FileInputStream(systemId);
> return new InputSource(fs);
> }
>}
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
_________________________________________________________________ Need more e-mail storage? Get 10MB with Hotmail Extra Storage. http://join.msn.com/?PAGE=features/es
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
_________________________________________________________________
Fast, faster, fastest: Upgrade to Cable or DSL today! https://broadband.msn.com
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
