带有jSoup的XML解析器 - 没有输出?

时间:2014-02-22 12:10:07

标签: java xml netbeans jsoup

此代码将一些详细信息发送到openvas服务器并接收XML作为回报。我试图从XML字符串中获取'id ='字符串。它似乎正在工作(即没有错误),但不会从'id'字符串输出内容。它几乎就像什么都没有。

我尝试将一个随机字符串添加到'id' - 这对jTextField6输出正常。

任何人都可以看到我的问题吗?谢谢!

    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如下:

<create_target_response id="b4c8de55-94d8-4e08-b20e-955f97a714f1" status_text="OK, resource created" status="201"></create_target_response>

1 个答案:

答案 0 :(得分:0)

Lob将只包含一个字符,是从流中读取的最后一个字符。以下是我的表现,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");

编辑 - 根据@Pshemo更新了StringBuilder。