简体   繁体   English

从Java中的URL读取XML文件

[英]Reading XML file from URL in java

I need to read the XML returned by API called in form of an URL, and convert in document format for further processing. 我需要读取以URL形式调用的API返回的XML,并转换为文档格式以进行进一步处理。

The URL is of form http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler . 该URL的格式为http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler I referred the answer at read xml from url and used the following code. 我引用了从url读取xml的答案,并使用了以下代码。 But the statement printed is "doc [#document: null]" . 但是打印的语句是“ doc [#document:null]” What mistake am I doing? 我在做什么错?

    String pre_apiURL = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=";        
    String apiURL = pre_apiURL + celeb + "";
    apiURL = apiURL.replaceAll(" ","%20");
    System.out.println("url "+apiURL);
    URL url = new URL(apiURL);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(url.openStream());

    System.out.println("doc " + doc.toString());

xyou can try like this, Here you can set your string response ang get xml string response into XML Document xyou可以这样尝试,在这里您可以设置字符串响应ang get xml字符串响应到XML文档

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document doc;
        try {
            builder = factory.newDocumentBuilder();
            doc =  builder.parse(new InputSource( new StringReader("your xml string response")));
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            ex.printStackTrace();
        }

i'm not sure but i think it's helpful to you. 我不确定,但我认为这对您有帮助。

This is can help you quite a bit: Transforming XML 这可以帮助您很多: 转换XML

But if you do not wish to go read that, I have inserted a code snippet of the entire code you require to get and display the xml from the URL: 但是,如果您不希望阅读这些内容,那么我已插入了获取并显示URL中xml所需的全部代码的代码段:

(Tried and Tested) (经过测试)


 import javax.xml.parsers.DocumentBuilder; import java.net.URL; import javax.xml.parsers.*; import org.w3c.dom.*; import javax.xml.transform.*; import java.io.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; public class Test{ public static void main(String[] args){ try{ String pre_apiURL = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler"; System.out.println("url "+ pre_apiURL); URL url = new URL(pre_apiURL); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(url.openStream()); printDocument(doc, System.out); }catch(Exception e){} } public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } } 


经过测试


All the best :) .. 祝一切顺利 :) ..

Let me know of the outcome. 让我知道结果。

Good luck! 祝好运!

Here doc is your document doc是您的文件

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM