我正在尝试解析xml文件但发生了Malformedurlexception
。你知道为什么会这样吗?
注意:在第一个代码段中有一行作为注释system.out.println(requestXml)
它正在工作(xml内容正在以字符串形式出现)但我想解析它但是解析方法不起作用。
代码
String requestXml = "";
try {
String argUrl = "http://192.168.1.25:3480/data_request?id=sdata&output_format=xml";
URL url = new URL(argUrl);
URLConnection con = url.openConnection();
// specify that we will send output and accept input
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(20000); // long timeout, but not infinite
con.setReadTimeout(20000);
con.setUseCaches(false);
con.setDefaultUseCaches(false);
// tell the web server what we are sending
con.setRequestProperty("Content-Type", "text/xml");
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(requestXml);
writer.flush();
writer.close();
// reading the response
InputStreamReader reader = new InputStreamReader(con.getInputStream());
StringBuilder buf = new StringBuilder();
char[] cbuf = new char[2048];
int num;
while (-1 != (num = reader.read(cbuf))) {
buf.append(cbuf, 0, num);
}
requestXml = buf.toString();
xmlParser(requestXml);
//System.out.println(requestXml);
} catch (Throwable t) {
t.printStackTrace(System.out);
}
return requestXml;
和xmlParser方法是:
protected void xmlParser(String xmlToBeParsed) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlToBeParsed);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("devices");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Stock Symbol: " + getValue("device name", element));
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
日志是:
04-30 11:55:21.299:W / System.err(826):java.net.MalformedURLException: 未找到协议:04-30 11:55:21.299: W / System.err的(826): 04-30 11:55:21.299:W / System.err(826):04-30 11:55:21.299: W / System.err(826):04-30 11:55:21.299:W / System.err(826):04-30 11:55:21.299: W / System.err(826):04-30 11:55:21.299: W / System.err(826):04-30 11:55:21.299: W / System.err(826):04-30 11:55:21.299:W / System.err(826): 04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826): 04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309: W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309: W / System.err的(826): 04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309:W / System.err(826):04-30 11:55:21.309: W / System.err(826):at java.net.URL。(URL.java:178)04-30 11:55:21.309:W / System.err(826):at java.net.URL。(URL.java:127) 04-30 11:55:21.309:W / System.err(826):at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:115) 04-30 11:55:21.319:W / System.err(826):at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:155) 04-30 11:55:21.319:W / System.err(826):at com.example.eos.SensorDataConnector.xmlParser(SensorDataConnector.java:93) 04-30 11:55:21.319:W / System.err(826):at com.example.eos.SensorDataConnector.getXMLData(SensorDataConnector.java:71) 04-30 11:55:21.319:W / System.err(826):at com.example.eos.EnergyOptimizer $ 5.onClick(EnergyOptimizer.java:148) 04-30 11:55:21.319:W / System.err(826):at android.view.View.performClick(View.java:4204)04-30 11:55:21.319: W / System.err(826):at android.view.View $ PerformClick.run(View.java:17355)04-30 11:55:21.319:W / System.err(826):at android.os.Handler.handleCallback(Handler.java:725)04-30 11:55:21.319:W / System.err(826):at android.os.Handler.dispatchMessage(Handler.java:92)04-30 11:55:21.319:W / System.err(826):at android.os.Looper.loop(Looper.java:137)04-30 11:55:21.319: W / System.err(826):at android.app.ActivityThread.main(ActivityThread.java:5041)04-30 11:55:21.319:W / System.err(826):at java.lang.reflect.Method.invokeNative(Native Method)04-30 11:55:21.319:W / System.err(826):at java.lang.reflect.Method.invoke(Method.java:511)04-30 11:55:21.319: W / System.err(826):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 04-30 11:55:21.319:W / System.err(826):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)04-30 11:55:21.329:W / System.err(826):at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:2)
您用来解析XML的是this method:
public Document parse(String uri) throws SAXException, IOException
它基本上期望从中获取内容的uri。这不是您的情况,因为您已经查询了URL并解析了内容。您没有提供该方法所期望的内容。
如果您跳过连接输入流的所有读取,我认为您应该没问题,只需执行以下操作:
InputStream input = con.getInputStream();
xmlParser(input);
注意我删除了很多行。然后:
protected void xmlParser(InputStream input)
{
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(input);