有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从soap响应中提取相同元素类型的列表?

如何从下面的XML中提取所有“BalDtoList”元素,例如从下面的soap响应中提取列表中的元素

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
    <ns:QueryAcctBal
        xmlns:ns="">
        <BalDtoList>
            <BalID>105529650</BalID>
            <AcctResID>1</AcctResID>
            <BalType>3</BalType>
            <Balance>9363</Balance>
            <EffDate>2014-04-30 15:24:31</EffDate>
        </BalDtoList>
        <BalDtoList>
            <BalID>2100000425</BalID>
            <AcctResID>5025</AcctResID>
            <BalType>1</BalType>
            <Balance>4560</Balance>
            <EffDate>2019-11-29 04:10:36</EffDate>
        </BalDtoList>
        <BalDtoList>
            <BalID>2100000413</BalID>
            <AcctResID>5008</AcctResID>
            <BalType>1</BalType>
            <Balance>60</Balance>
            <EffDate>2019-11-29 04:10:36</EffDate>
        </BalDtoList>
        <BalDtoList>
            <BalID>2100000401</BalID>
            <AcctResID>5011</AcctResID>
            <BalType>9</BalType>
            <Balance>80</Balance>
            <EffDate>2019-11-29 04:10:36</EffDate>
        </BalDtoList>
        <ResultMsg>success</ResultMsg>
    </ns:QueryAcctBal>
</soapenv:Body>

下面是我的代码函数,在这里我从SOAP消息中提取属性

    public static void ExtractingWalletInfo(String message,String mobno) {
    try {

        InputStream is = new ByteArrayInputStream(message.getBytes());
        SOAPMessage respMsg = MessageFactory.newInstance().createMessage(null, is);
        SOAPBody body = respMsg.getSOAPBody();
        NodeList Bal = body.getElementsByTagName("BalDtoList");

    }catch(Exception e){
        e.printStackTrace();
    }

}

共 (1) 个答案

  1. # 1 楼答案

    Web服务客户端(SOAP)

    如果必须访问SOAPweb服务接口,请使用JAX-WS(随JDK提供,jdk/bin/wsimport)、Apache AxisApache CXF或其他框架来生成web服务客户端

    解析XML

    如果您没有访问SOAP接口的权限,但只是尝试解析XML文档(如SOAP信封),请使用DocumentBuilder,例如:

    DocumentBuilder documentBuilder =
        DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Reader in = new StringReader(message);
    Document document = documentBuilder.parse(new InputSource(in));
    in.close();
    NodeList nodes = document.getElementsByTagName("BalDtoList");
    for(int i = 0; i < nodes.getLength(); i++) {
        Element elem = (Element) nodes.item(i);
        System.out.println(i + ":");
        for (String name : new String[] {
            "BalID", "AcctResID", "BalType", "Balance", "EffDate"
        }) {
            String value = elem.getElementsByTagName(name).item(0).getTextContent();
            System.out.println(name + " = " + value);
        }
    }
    

    输出是

    0:
    BalID = 105529650
    AcctResID = 1
    BalType = 3
    Balance = 9363
    EffDate = 2014-04-30 15:24:31
    1:
    BalID = 2100000425
    AcctResID = 5025
    BalType = 1
    Balance = 4560
    EffDate = 2019-11-29 04:10:36
    ...
    

    顺便说一句,您的XML文档无效,因为它缺少根元素的结束标记</soapenv:Envelope>