在java中从URL读取XML文件
我需要读取API以URL的形式调用返回的XML,并将其转换为文档格式以便进一步处理 URL的格式为在java中从URL读取XML文件,java,xml,Java,Xml,我需要读取API以URL的形式调用返回的XML,并将其转换为文档格式以便进一步处理 URL的格式为http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler。我在上引用了答案,并使用了以下代码。但是打印的语句是“doc[#document:null]”。我犯了什么错误 String pre_apiURL = "http://lo
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler
。我在上引用了答案,并使用了以下代码。但是打印的语句是“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());
这里是您的文件
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", "");
这里是您的文件
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", "");
你可以这样试试,
在这里,您可以设置字符串响应并将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();
}
我不确定,但我认为这对你有帮助。x你可以这样试试,
在这里,您可以设置字符串响应并将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();
}
我不确定,但我认为这对你有帮助。这对你很有帮助: 但是,如果您不想阅读,我已经插入了从URL获取和显示xml所需的全部代码的代码片段: (经过考验)
import javax.xml.parsers.DocumentBuilder;
导入java.net.URL;
导入javax.xml.parsers.*;
导入org.w3c.dom.*;
导入javax.xml.transform.*;
导入java.io.*;
导入javax.xml.transform.stream.*;
导入javax.xml.transform.dom.*;
公开课考试{
公共静态void main(字符串[]args){
试试{
字符串pre_apirl=”http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler";
System.out.println(“url”+pre_apiURL);
URL URL=新URL(pre_apiURL);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
documentdoc=db.parse(url.openStream());
打印文档(单据、系统输出);
}捕获(例外e){}
}
public static void printDocument(Document doc,OutputStream out)抛出IOException、TransformerException{
TransformerFactory tf=TransformerFactory.newInstance();
变压器=tf.新变压器();
setOutputProperty(OutputKeys.OMIT_XML_声明,“no”);
setOutputProperty(OutputKeys.METHOD,“xml”);
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(OutputKeys.ENCODING,“UTF-8”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}缩进金额,“4”);
transformer.transform(新的DOMSource(doc)),
新的StreamResult(新的OutputStreamWriter(输出,“UTF-8”));
}
}
这对您有很大帮助:
但是,如果您不想阅读,我已经插入了从URL获取和显示xml所需的全部代码的代码片段:
(经过考验)
import javax.xml.parsers.DocumentBuilder;
导入java.net.URL;
导入javax.xml.parsers.*;
导入org.w3c.dom.*;
导入javax.xml.transform.*;
导入java.io.*;
导入javax.xml.transform.stream.*;
导入javax.xml.transform.dom.*;
公开课考试{
公共静态void main(字符串[]args){
试试{
字符串pre_apirl=”http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler";
System.out.println(“url”+pre_apiURL);
URL URL=新URL(pre_apiURL);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
documentdoc=db.parse(url.openStream());
打印文档(单据、系统输出);
}捕获(例外e){}
}
public static void printDocument(Document doc,OutputStream out)抛出IOException、TransformerException{
TransformerFactory tf=TransformerFactory.newInstance();
变压器=tf.新变压器();
setOutputProperty(OutputKeys.OMIT_XML_声明,“no”);
setOutputProperty(OutputKeys.METHOD,“xml”);
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(OutputKeys.ENCODING,“UTF-8”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}缩进金额,“4”);
transformer.transform(新的DOMSource(doc)),
新的StreamResult(新的OutputStreamWriter(输出,“UTF-8”));
}
}
toString
只是打印有关DOM的信息,而不是XML内容本身,您需要使用转换器
再次将XML写出来,类似于或您的代码是正确的。只需在doc对象的帮助下解析xml即可。toString不打印xml。toString
只是打印有关DOM的信息,而不是xml内容本身,您需要使用Transformer
再次将xml写出来,或者您的代码是正确的。只需在doc对象的帮助下解析xml即可。toString不会打印xml。这太棒了!如何选择要显示的xml属性类型?如何将xml字符串作为返回类型?这太棒了!如何选择要显示的xml属性类型?如何将xml字符串作为返回类型获取?