Hi Rob,

You're setting your EntityResolver after calling parse. The entire document
(and DTD) has already been read at that point. You need to set it before.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]

Rob Davis-5 <[EMAIL PROTECTED]> wrote on 03/03/2008 10:37:21 AM:

> Hi, I want to read in and validate an XML file against a local DTD.
>
> I have created a class based on EntityResolver which plugs into the
Parser.
>
> However, this seems to be ignored because the parser still attempts to
> connect over the internet to w3c which I do not want. I know this because
of
> the exception I get when it attempts (see below)
>
> See also below my code.
>
> Please advise
>
> java.net.ConnectException: Connection refused: connect
>    at java.net.PlainSocketImpl.socketConnect(Native Method)
>    at java.net.PlainSocketImpl.doConnect(Unknown Source)
>    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
>    at java.net.PlainSocketImpl.connect(Unknown Source)
>    at java.net.Socket.connect(Unknown Source)
>    at java.net.Socket.connect(Unknown Source)
>    at sun.net.NetworkClient.doConnect(Unknown Source)
>    at sun.net.www.http.HttpClient.openServer(Unknown Source)
>    at sun.net.www.http.HttpClient.openServer(Unknown Source)
>    at sun.net.www.http.HttpClient.<init>(Unknown Source)
>    at sun.net.www.http.HttpClient.New(Unknown Source)
>    at sun.net.www.http.HttpClient.New(Unknown Source)
>    at
sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown
> Source)
>    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown
Source)
>    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
>    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
> Source)
>    at
> org.apache.xerces.impl.XMLEntityManager.
> setupCurrentEntity(XMLEntityManager.java:986)
>    at
>
org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:897)

>    at
> org.apache.xerces.impl.XMLEntityManager.
> startDTDEntity(XMLEntityManager.java:864)
>    at
> org.apache.xerces.impl.XMLDTDScannerImpl.
> setInputSource(XMLDTDScannerImpl.java:241)
>    at
> org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.
> dispatch(XMLDocumentScannerImpl.java:1001)
>    at
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.
> scanDocument(XMLDocumentFragmentScannerImpl.java:324)
>    at
> org.apache.xerces.parsers.XML11Configuration.
> parse(XML11Configuration.java:845)
>    at
> org.apache.xerces.parsers.XML11Configuration.
> parse(XML11Configuration.java:768)
>    at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:108)
>    at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:230)
>    at SerializeTester.test(SerializeTester.java:30)
>    at SerializeTester.main(SerializeTester.java:62)
>
>
>
> import java.io.File;
> import java.net.MalformedURLException;
> import java.net.URL;
>
> import org.xml.sax.EntityResolver;
> import org.xml.sax.InputSource;
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.OutputStream;
> import org.xml.sax.InputSource;
> import org.w3c.dom.Document;
> // Parser import
> import org.apache.xerces.parsers.DOMParser;
>
>
>
> public class SerializeTester
> {
>   // File to read XML from
>   private File inputXML;
>
>   // File to serialize XML to
>   private File outputXML;
>
>   public SerializeTester(File inputXML)
>   {
>    this.inputXML = inputXML;
>   }
>
>   public void test(OutputStream outputStream) throws Exception
>   {
>    DOMParser parser = new DOMParser( );
>
>    // Get the DOM tree as a Document object
>    parser.parse(new InputSource(
>    new FileInputStream(inputXML)));
>
>     LocalDTDResolver LocalDTDResolver
>     = new LocalDTDResolver(
> "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";,
>                             new File("C:/svgdtd/svg11.dtd")
>                              );
>
>
>     parser.setEntityResolver( LocalDTDResolver );
>
>    Document doc = parser.getDocument( );
>    // Serialize
>
>    org.w3c.dom.Element w3cSvgTextElement = doc.getElementById(
"LABEL_OUTLINE"
> );
>
>   }
>
>   public static void main(String[] args)
>   {
>    if (args.length != 2)
>    {
>      System.out.println(
>        "Usage: java javaxml3.SerializeTester " +
>        "[XML document to read] " +
>        "[filename to write output to]");
>      return;
>    }
>    try
>    {
>      SerializeTester tester =
>        new SerializeTester(new File(args[0]));
>      tester.test(new FileOutputStream(new File(args[1])));
>    }
>    catch (Exception e)
>    {
>       e.printStackTrace( );
>    }
>   }
> }
>
>
>
>
>
>  public class LocalDTDResolver implements EntityResolver {
>
>    String mySystemIdToIntercept;
>    File myLocalDtdPath;
>    URL localDtdFileAsUrl;
>
>    public LocalDTDResolver( String systemIdToIntercept, File localDtdPath
)
> throws MalformedURLException
>    {
>       mySystemIdToIntercept = systemIdToIntercept;
>       myLocalDtdPath = localDtdPath;
>
>        localDtdFileAsUrl = myLocalDtdPath.toURI().toURL();
>    }
>
>    public InputSource resolveEntity (String publicId, String systemId)
>    {
>      if (systemId.equals( mySystemIdToIntercept ))
>      {
>        return new InputSource( localDtdFileAsUrl.toString() );
>      }
>      else
>      {
>        // use the default behaviour (?)
>        return null;
>      }
>    }
>  }
> --
> View this message in context: http://www.nabble.com/Xerces-DOM-
> parser-ignores-EntityResolver-providing-local-DTD-for-validation-
> tp15806529p15806529.html
> Sent from the Xerces - J - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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]

Reply via email to