gpt4 book ai didi

java - 在 java MalformedurException 中解析 XML

转载 作者:行者123 更新时间:2023-12-02 07:04:03 25 4
gpt4 key购买 nike

我正在尝试解析 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: Protocol not found: 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(Native Method)

最佳答案

您用来解析 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);

关于java - 在 java MalformedurException 中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16299445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com