java: xml 解析并获取属性

java: xml parse and get properties

我有一些 url

关注 xml
<FmicResponse xmlns="http://fmic.dat.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Status>OK</Status>
<EstimateId i:nil="true"/>
<FmicAllInContractCPL25>311.08533916789725</FmicAllInContractCPL25>
<FmicAllInContractCPL50>388.95855941609693</FmicAllInContractCPL50>
<FmicAllInContractCPL75>476.07103709203955</FmicAllInContractCPL75>
<FmicLineHaulContractCPL25>293.96033916789725</FmicLineHaulContractCPL25>
<FmicLineHaulContractCPL50>371.83355941609693</FmicLineHaulContractCPL50>
<FmicLineHaulContractCPL75>458.94603709203955</FmicLineHaulContractCPL75>
<FmicFsc>17.125</FmicFsc>

</FmicResponse>

我想解析它并获取属性

所以我的解析器是这样的

  URL url = new URL(urlAddress);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(url.openStream());
           
            NodeList list = doc.getElementsByTagName("FmicResponse");

            for (int temp = 0; temp < list.getLength(); temp++) {

                Node node = list.item(temp);

                NodeList childNodes = node.getChildNodes();

                for (int cd = 0; cd < childNodes.getLength(); cd++) {
                    Node node1 = list.item(cd);
                    NamedNodeMap attributesList = node1.getAttributes();

                    for (int j = 0; j < attributesList.getLength(); j++) {
                        System.out.println("Attribute: "
                                + attributesList.item(j).getNodeName() + " = "
                                + attributesList.item(j).getNodeValue());
                    }

                }

            }

但属性 return 为空或只是

<FmicResponse xmlns="http://fmic.dat.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

无法访问属性。

请帮忙

将 getTextContent() 与 ELEMENT_NODE 一起使用:

    NodeList fmicResponseList = doc.getElementsByTagName("FmicResponse");
    for (int i = 0; i < fmicResponseList.getLength(); i++) {
        Node fmicResponse = fmicResponseList.item(i);

        NodeList childNodes = fmicResponse.getChildNodes();
        for (int cd = 0; cd < childNodes.getLength(); cd++) {
            Node child = childNodes.item(cd);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(child.getNodeName() + ": " + child.getTextContent());
            }
        }
    }

或者,您可以使用 Jackson 解串器。在这种情况下,您只需像这样定义 class:

 @JacksonXMLRootElement(namespace="http://fmic.dat.com", localName="FmicResponse"
public class FmicResponse{
    
    private String Status;
    
    private String FmicAllInContractCPL25;
    
    private String FmicAllInContractCPL50;
    
    private String FmicAllInContractCPL75;
    
    private String FmicLineHaulContractCPL25;
    
    private String FmicLineHaulContractCPL50;
    
    private String FmicLineHaulContractCPL75;
    
    private String FmicAllInContractCPL25;
    
    private String FmicFsc;
    
    @JacksonXmlProperty(namespace = "http://www.w3.org/2001/XMLSchema-instance", localName = "EstimateId")
    private String EstimateId;
    
}

然后使用类似这样的方式读取 XML:

XMLInputFactory f = XMLInputFactory.newFactory();
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(inputFile));

XmlMapper mapper = new XmlMapper();
sr.next(); // point to element FmicResponse
FmicResponse fmicResponse = mapper.readValue(sr, FmicResponse.class);
sr.close();