[英]XML Parser with jSoup - No output?
This code sends some details to an openvas server and receives XML in return. 此代码将一些详细信息发送到openvas服务器,并返回XML。 I am trying to take the 'id=' string from the XML string. 我正在尝试从XML字符串中获取'id ='字符串。 It seems to be working (ie no errors) but won't output contents from the 'id' String. 它似乎正在工作(即没有错误),但是不会从“ id”字符串中输出内容。 Its almost like nothing is in it. 它几乎就像里面什么都没有。
I tried adding a random string to 'id' - this outputs fine to the jTextField6. 我尝试在'id'中添加一个随机字符串-这对jTextField6输出很好。
Can anyone see my issue?? 谁能看到我的问题? Thanks! 谢谢!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String TargIP = jTextField1.getText(); // Get IP Address
String TargName = jTextField5.getText(); // Get Target Name
String Vag = "8d32ad99-ac84-4fdc-b196-2b379f861def";
String Lob = "";
final String dosCommand = "cmd /c omp -u admin -w admin --xml=\"<create_target><name>" + TargName + "</name><hosts>" + TargIP + "</hosts></create_target>\"";
final String location = "C:\\";
try {
final Process process = Runtime.getRuntime().exec(
dosCommand + " " + location);
final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1) {
System.out.print((char)ch);
Lob = String.valueOf((char)ch);
jTextArea2.append(Lob);
}
String id = Jsoup.parse(Lob).getAllElements().attr("id"); // This may be the issue
jTextField6.setText(id);
} catch (IOException e) {
e.printStackTrace();
}
String Lob is as follows: 字符串吊球如下:
<create_target_response id="b4c8de55-94d8-4e08-b20e-955f97a714f1" status_text="OK, resource created" status="201"></create_target_response>
Lob
will contain just one character, being the last one read from the stream. Lob
将仅包含一个字符,这是从流中读取的最后一个字符。 Here's how I would do it, FWIW: FWIW,我会这样做的:
StringBuilder fullResponse = new StringBuilder();
while((ch = in.read()) != -1) {
System.out.print((char)ch);
fullResponse.append(String.valueOf((char)ch));
}
jTextArea2.append(fullResponse.toString());
String id = Jsoup.parse(fullResponse.toString()).getAllElements().attr("id");
Edit - Updated with StringBuilder as per @Pshemo. 编辑 -按照@Pshemo使用StringBuilder更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.