在java中从URL读取XML文件

时间:2015-04-15 06:50:45

标签: java xml

我需要读取以URL形式调用的API返回的XML,并以文档格式进行转换以便进一步处理。

网址格式为http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler。我在read xml from url引用了答案并使用了以下代码。但印刷的声明是" doc [#document:null]" 。我在做什么错误?

    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());

3 个答案:

答案 0 :(得分:2)

你可以这样试试, 在这里,您可以将字符串响应ang get xml string response设置为XML Document

        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();
        }

我不确定,但我认为这对你有帮助。

答案 1 :(得分:2)

这可以帮到你很多:Transforming XML

但如果您不想阅读,我已经插入了您需要的整个代码的代码片段,并从URL中显示xml:

(尝试和测试)




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")));
	}

}





Tested


一切顺利:) ..

让我知道结果。

祝你好运!

答案 2 :(得分:1)

此处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", "");