读取xml没有给出正确的值

时间:2018-10-03 03:58:39

标签: java xml

我正在尝试获取orderCode的值

public class ReadXml {
    static Map<String, String> map = new HashMap<>();   
    static List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    public List<HashMap<String, String>> readxml() throws ParserConfigurationException, SAXException, IOException{
        File file = new File(
                "./resources/XMLFile/test.xml");

        // Make an instance of the DocumentBuilderFactory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(file);
        dom.getDocumentElement().normalize();
        dom.getDocumentElement().getNodeName();

        if (dom.hasChildNodes()) {

        getNote(dom.getChildNodes()); // now re run
        }
        return list;
    }

    private static List<HashMap<String, String>> getNote(NodeList nodeList) {

        Node node =null;
        for (int count = 0; count < nodeList.getLength(); count++) {
            HashMap<String, String> data = new HashMap<>();
            Node tempNode = nodeList.item(count);

            // make sure it's element node.
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

                if (tempNode.hasAttributes()) {

                    //get attributes names and values
                    NamedNodeMap nodeMap = tempNode.getAttributes();

                    for (int i = 0; i < nodeMap.getLength(); i++) {

                        node = nodeMap.item(i);
                        node.getNodeName();  // 
                        node.getNodeValue();
                    }
                }
            }

            if (tempNode.hasChildNodes()) {

                // loop again if has child nodes
                getNote(tempNode.getChildNodes());

            }

            data.put(tempNode.getNodeName(), tempNode.getTextContent());            

            list.add(data);
        }
        return list;
    }

}

Xml文件如下:

 <submit> <shortCode>NTT</shortCode> 
     <order orderCode="TEST/TEST192/2018SEP30/095552"> 
      <description>RegressionTesting</description>
      <amount currencyCode="abc"/>
      debitCreditIndicator="test" exponent="2" value="123"/>

我的问题是,它没有提供OrderCode的值。

1 个答案:

答案 0 :(得分:0)

一个词,xPath

因此,将您的XML修改为...

<submit> 
    <shortCode>NTT</shortCode> 
    <order orderCode="TEST/TEST192/2018SEP30/095552"> 
        <description>RegressionTesting</description>
        <amount currencyCode="abc"/>
    </order>
</submit>

并使用...

try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile("/submit/order");

    Node node = (Node) exp.evaluate(doc.getFirstChild(), XPathConstants.NODE);
    if (node != null) {
        Node value = node.getAttributes().getNamedItem("orderCode");
        System.out.println(value.getTextContent());
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

哪个会打印...

 TEST/TEST192/2018SEP30/095552

或者我们可以直接进入属性本身...

try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile("/submit/order/@orderCode");
    String value = (String) exp.evaluate(doc.getFirstChild(), XPathConstants.STRING);
    System.out.println(value);
} catch (Exception ex) {
    ex.printStackTrace();
}

哪个会打印...

 TEST/TEST192/2018SEP30/095552

我还考虑查看Java API for XML Processing (JAXP)以获得更多详细信息