【问题标题】:How to extract value from a specified string : java如何从指定的字符串中提取值:java
【发布时间】:2014-02-21 11:32:46
【问题描述】:

我有一个类似下面的字符串:

<output transactionId="53264089"
        paymentId="21575285"
        amount="52.78"
        dateTime="2013-12-13 15:04:42"
        mode="TEST"
        referenceNo="80001186"
        transactionType="Authorized"
        status="Processed"
        isFlagged="NO" />

并想提取值:

transactionId;
paymentId;
amount;
dateTime;
mode;
referenceNo;
transactionType;
status;
isFlagged;

在java中我该怎么做?

实际上它是来自第三方服务器的响应,并且真的不知道如何从响应中获取值。

谢谢

【问题讨论】:

  • 通过xml解析器解析
  • 也试试这个教程。 Tutorial

标签: java xml split extract


【解决方案1】:

在我看来就像一个 XML 节点。我认为您正在寻找的是如何读取节点中的 XML 属性。

This 应该可以帮到你。

【讨论】:

    【解决方案2】:

    例如,通过以下代码:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Example {
         public static void main(String[] args) {
             String input = "<output transactionId=\"53264089\" paymentId=\"21575285\" " +
                    "amount=\"52.78\" dateTime=\"2013-12-13 15:04:42\" " +
                        "mode=\"TEST\" referenceNo=\"80001186\" transactionType=\"Authorized\" " +
                            "status=\"Processed\" isFlagged=\"NO\"/>";
    
             Pattern pattern = Pattern.compile("\"([^\"]*)\"");
             Matcher matcher = pattern.matcher(input);
    
             while(matcher.find()) {
               System.out.println(matcher.group(1));
             }   
         }
    }
    

    它给出:

    53264089
    21575285
    52.78
    2013-12-13 15:04:42
    TEST
    80001186
    Authorized
    Processed
    NO
    

    【讨论】:

    • 谢谢,你是救命稻草。
    【解决方案3】:
    String str ="<output transactionId=\"53264089\" paymentId=\"21575285\" amount=\"52.78\"   dateTime=\"2013-12-13 15:04:42\" mode=\"TEST\" referenceNo=\"80001186\" transactionType=\"Authorized\" status=\"Processed\" isFlagged=\"NO\" />";
    String[] values = str.split("\"");
    for(int i=1;i<values.length;i+=2)
    System.out.println(values[i]);
    

    输出:

     53264089
     21575285
     52.78
     2013-12-13 15:04:42
     TEST
     80001186
     Authorized
     Processed
     NO
    

    【讨论】:

      【解决方案4】:

      您可以使用Jsoup 作为 xml 解析器。这很简单:

      1. 选择output元素,
      2. 从创立中挑选第一个,
      3. 获取其属性
      4. 遍历所有属性

      这里是代码示例

      String xml = "<output transactionId=\"53264089\"\r\n" + 
              "        paymentId=\"21575285\"\r\n" + 
              "        amount=\"52.78\"\r\n" + 
              "        dateTime=\"2013-12-13 15:04:42\"\r\n" + 
              "        mode=\"TEST\"\r\n" + 
              "        referenceNo=\"80001186\"\r\n" + 
              "        transactionType=\"Authorized\"\r\n" + 
              "        status=\"Processed\"\r\n" + 
              "        isFlagged=\"NO\" />";
      
      
      Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
      Attributes attr = doc.select("output").first().attributes();
      for (Attribute a : attr)
          System.out.printf("%-15s -> %s%n", a.getKey(), a.getValue());
      

      输出:

      transactionid   -> 53264089
      paymentid       -> 21575285
      amount          -> 52.78
      datetime        -> 2013-12-13 15:04:42
      mode            -> TEST
      referenceno     -> 80001186
      transactiontype -> Authorized
      status          -> Processed
      isflagged       -> NO
      

      【讨论】:

        猜你喜欢
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 2019-10-15
        相关资源
        最近更新 更多