Tag: doctype

读取mybatis xml文件时会出现“java.net.UnknownHostException”

我喜欢通过java代码从mybatis xml文件中读取数据。 (不是mybatis映射,只是想从应用程序外部读取数据。) SELECT id,name,standard,age,sex FROM user; 所以我尝试使用xml阅读器如下: File fXmlFile = new File(“c:\\mybatis.xml”); DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName(“mapper”); . . . 但是,当我运行此代码时,我得到了以下错误 java.net.UnknownHostException: mybatis.org at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) […]

如何在编写XML文件时忽略DTDvalidation但保留Doctype?

我正在研究一个系统,该系统应该能够读取任何(或至少是任何格式良好的)XML文件,操作一些节点并将它们写回到同一个文件中。 我希望我的代码尽可能通用,我不想要 在我的代码中的任何位置对模式/ Doctype信息进行硬编码引用。 doctype信息位于源文档中,我想保留该doctype信息,而不是在我的代码中再次提供。 如果文档没有DocType,我不会添加一个。 除了我的几个节点之外,我根本不关心这些文件的forms或内容。 自定义EntityResolvers或StreamFilters以省略或以其他方式操纵源信息(已经遗憾的是,命名空间信息似乎无法从声明它的文档文件中访问,但我可以使用uglier XPath进行管理) DTDvalidation。 我没有引用的DTD,我不想包含它们,并且在不知道它们的情况下完全可以进行节点操作。 目的是使源文件完全不变,除了通过XPath检索的已更改的节点。 我想逃避标准的javax.xml。 我到目前为止的进展: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setAttribute(“http://xml.org/sax/features/namespaces”, true); factory.setAttribute(“http://xml.org/sax/features/validation”, false); factory.setAttribute(“http://apache.org/xml/features/nonvalidating/load-dtd-grammar”, false); factory.setAttribute(“http://apache.org/xml/features/nonvalidating/load-external-dtd”, false); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(false); factory.setIgnoringComments(false); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(inStream)); 这会成功将XML源加载到org.w3c.dom.Document中,忽略DTDvalidation。 我可以做我的替换然后我用 Source source = new DOMSource(document); Result result = new StreamResult(getOutputStream(getPath())); // Write the DOM […]

使用DOM解析xml,DOCTYPE将被删除

在编辑xml时,如何用java擦除doctype? 得到这个xml文件: <!DOCTYPE map[ ]> test1 test1 test1 我的function很基础: public static void EditStationName(int id, InputStream is, String path, String name) throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(is); Element e = dom. getElementById(String.valueOf(id)); e.setTextContent(name); // Write the DOM document to the file Transformer xformer = […]

如何在java的xpath中在运行时禁用dtd?

我在文件中得到了dtd,我无法将其删除。 当我尝试用Java解析它时,我得到“引起:java.net.SocketException:网络无法访问:连接”,因为它的远程dtd。 我能以某种方式禁用dtd检查吗?