XML Parser with jSoup - No output?



此代码将一些详细信息发送到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();
  }

字符串 Lob 如下所示:

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

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更新。

最新更新