when parsing an XML file, what to do when you encounter the definition of a DTD.
The following code is the Ibatis configuration file, which is checked from the HTTP://WWW.IBATIS.COM/DTD/SQL-MAP-CONFIG-2.DTD address for the corresponding DTD file, in the same way as any parsing method. If this address does not exist on the internet, it will be reported to Java.net.ConnectException:Connection timed Out:connect.
XML code: XML Version = "1.0" encoding = "UTF-8"?> <! doctype sqlmapconfig public "-//ibatis.com//dtd sql map config 2.0//en" "HTTP://WWW.IBATIS.COM/DTD/SQL-MAP-CONFIG-2.DTD" > < sqlmapconfig > < Settings Usestatementnamespaces = "true"/> <!--prevent null SQLMAP error by zhangbo start --> < sqlmap resource = "Ibatis/project/project-blank.xml"/> & nbsp < Sqlmap Resource = "Ibatis/project/user-mapping.xml"/> </sqlmapconfig >
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE sqlmapconfig Public "-//ibatis.com//dtd SQL Map Config 2.0//en" "http://www.ibatis.com/dtd/ Sql-map-config-2.dtd ">
<sqlMapConfig>
<settings usestatementnamespaces=" true "/>
< !--prevent null Sqlmap error by Zhangbo start-->
<sqlmap resource= "Ibatis/project/project-blank.xml"/>
< Sqlmap resource= "Ibatis/project/user-mapping.xml"/>
</sqlMapConfig>
There are two ways to resolve this problem: the first is to download the DTD locally, specify to load the DTD file locally, and the other, simply ignore the DTD file.
Both of these approaches are based on the solution of rebuilding the entity's Parser (Entityresolver), rebuild the entity parser instead of using the default entity parser, because the default parser reads based on the actual URL, and here's how to parse the configuration file mentioned above. There are two ways of tagging.
Java code public void addtoibatistotleconfig () { try { DocumentBuilderFactory factory = DocumentBuilderFactory . Newinstance (); Factory.setignoringelementcontentwhitespace (true); documentbuilder db = factory.newdocumentbuilder (); db.setentityresolver (New entityreSolver () { public Inputsource resolveentity (String publicid, string systemid) throws SAXException, IOException { if (PublicId.equals ("-// ibatis.com//dtd sql map config 2.0//en ") { //This is the first way to omit the DTD and create a new default NULL XML to replace &nbsP; return New inputsource (New bytearrayinputstream ("<?xml Version= ' 1.0 ' encoding= ' GBK '?> '. GetBytes ()); //This is the second way to specify a local DTD file, which, of course, requires downloading the XML files locally, into the corresponding directories // inputstream dtd_stream = this.getclass (). getResourceAsStream (".. Dtd/sql-map-config-2.dtd "); // Return new inputsource (Dtd_stream); &nbsP } return NULL; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}}); string ibatistotalconfigfilename = ctx.getibatistotalconfigfile () ; File Ibatistotalconfigfile = new file (ibatistotalconfigfilename); &Nbsp; document xmldoc = db.parse ("file://localhost//" + Ibatistotalconfigfilename); element root = xmldoc.getdocumentelement (); } catch (exception e) { e.printstacktrace (); } }
public void Addtoibatistotleconfig () {try {documentbuilderfactory factory = documentbuilderfactory. Newinstanc
E ();
Factory.setignoringelementcontentwhitespace (TRUE);
Documentbuilder db = Factory.newdocumentbuilder (); Db.setentityresolver (New Entityresolver () {Public InputSource resolveentity (string publicid, String systemid) throws SA
Xexception, IOException {if (Publicid.equals ("-//ibatis.com//dtd SQL Map Config 2.0//en")) { This is the first way to ignore the DTD and create a new default empty XML instead of return new InputSource ("<?xml version= ' 1.
0 ' encoding= ' GBK '?> '. getBytes ()); This is the second way to specify a local DTD file, which, of course, needs to download the XML file locally, into the corresponding Directory//InputStream Dtd_stream = This.getclass (). getresource Asstream ("..
Dtd/sql-map-config-2.dtd ");
return new InputSource (Dtd_stream);
return null;
}}); String Ibatistotalconfigfilename = Ctx.getibatistotaLconfigfile ();
File Ibatistotalconfigfile = new file (ibatistotalconfigfilename);
Document xmldoc = Db.parse ("file://localhost//" +ibatistotalconfigfilename);
Element root = Xmldoc.getdocumentelement ();
catch (Exception e) {e.printstacktrace (); }
}
The DTD definition will be lost if you use W3cdom to edit the above XML file and convert document to an XML file.
When you add a DTD definition, you want to work in the converter, focusing on this two-line code: Java code transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, Doc.getdoctype (). Getpublicid ()); Transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, Doc.getdoctype (). Getsystemid ());
Transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, Doc.getdoctype (). Getpublicid ());
Transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, Doc.getdoctype (). Getsystemid ());
The value of the DTD definition is available in the Document object, you can output it directly, or you can change it.
Here are my practical applications.
Java Code import java.io.File; import java.io.FileNotFoundException; Import & nbsp;java.io.fileoutputstream; Import javax.xml.transform.Transformer; Import javax.xml.transform.TransformerConfigurationException; Import javax.xml.transform.transformerexception; Import javax.xml.transform.TransformerFactory; Import javax.xml.transform.dom.DOMSource; Import javax.xml.transform.stream.streamresult; Import javax.xml.xpath.XPath; Import javax.xml.xpath.xpathconstants; Import javax.xml.xpath.XPathExpressionException; Import javax.xml.xpath.XPathFactory; Import org.w3c.dom.Document; Import org.w3c.dom.Node; Public static void savexml (string filename, document doc) {// output document to file TransformerFactory Transfactory = transformerfactory.newinstance (); try { Transformer Transformer = transfactory.newtransformer (); transformer.setoutputproperty ("Indent" , "yes"); domsource source = new domsource (); source.setnode (DOC); system.out.println (Doc.getdoctype (). GetPublicId ()); Transformer.setoutputproperty (Javax.xml.transform.outputkeys.doctype_public, doc.getdoctype () getPublicId ()); transformer.setoutputproperty (javax.xml.transform.outputkeys.doctype_system, Doc.getdoctype (). Getsystemid ()); &nbSp Streamresult result = new streamresult (); result.setoutputstream (New FileOutputStream (fileName)); Transformer.transform (source, result); } catch (transformerconfigurationexception e) { e.printstacktrace (); } catch (transformerexception e) { &nbSp; e.printstacktrace (); } catch ( Filenotfoundexception e) { e.printstacktrace (); } }
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerConfigurationException;
Import javax.xml.transform.TransformerException;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;
Import Javax.xml.xpath.XPath;
Import javax.xml.xpath.XPathConstants;
Import javax.xml.xpath.XPathExpressionException;
Import Javax.xml.xpath.XPathFactory;
Import org.w3c.dom.Document;
Import Org.w3c.dom.Node; public static void SaveXML (String fileName, document DOC) {//Document output to file transformerfactory transfactory =
Transformerfactory.newinstance ();
try {Transformer Transformer = Transfactory.newtransformer ();
Transformer.setoutputproperty ("Indent", "yes");
Domsource Source = new Domsource (); Source.setnode (DOC);
System.out.println (Doc.getdoctype (). Getpublicid ());
Transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, Doc.getdoctype (). Getpublicid ());
Transformer.setoutputproperty (Javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, Doc.getdoctype (). Getsystemid ());
Streamresult result = new Streamresult ();
Result.setoutputstream (FileName) (new FileOutputStream);
Transformer.transform (source, result);
catch (Transformerconfigurationexception e) {e.printstacktrace ();
catch (Transformerexception e) {e.printstacktrace ();
catch (FileNotFoundException e) {e.printstacktrace (); }
}