2

Currently I have a java application that loads XML from a local file into a string. My code looks like this

     private String xmlFile = "D:\\mylocalcomputer\\extract-2339393.xml";
     String fileStr = FileUtils.readFileToString(new File(xmlFile));

How can I get the contents of the XML file if it was located on the internet, at a URL like http://mydomain.com/xml/extract-2000.xml ?

4

2 回答 2

2

试试萨克斯接口

private String xmlURL = "http://mydomain.com/xml/extract-2000.xml";

XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(handler);
reader.parse(new InputSource(new URL(xmlURL).openStream()));

有关 SAX 的更多信息,请查看此链接

于 2012-01-16T06:14:01.267 回答
2

检查此代码:

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  InputStream inputStream = new FileInputStream(new File("http://mydomain.com/xml/extract-2000.xml"));
  org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
  StringWriter stw = new StringWriter();
  Transformer serializer = TransformerFactory.newInstance().newTransformer();
  serializer.transform(new DOMSource(doc), new StreamResult(stw));
  stw.toString(); 
于 2012-01-16T06:32:39.193 回答