如何在xml获取标记名称

时间:2017-12-12 15:22:09

标签: java xml

enter image description here

以上是我的XMl ......

如何获取元素标记的名称,如ReportDatePeriodEndingDateFileDate等等?

List<String> stkCompFinaciallist = new ArrayList<String>();

        File fXmlFile = new File(filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        // optional, but recommended
        // read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getDocumentElement().getElementsByTagName("BalanceSheetEntity");
        out.println("length=" + nList.getLength());

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

            Node nNode = nList.item(temp);

            out.println("\nCurrent Element :" + nNode.getNodeName());
            //out.println("node name:"+ nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;


            if (eElement.getElementsByTagName("ReportDate").getLength() > 0) {
                    stkCompFinaciallist.add(eElement.getElementsByTagName("ReportDate").item(0).getFirstChild().getNodeValue());
                    out.println("ReportDate : " + eElement.getElementsByTagName("ReportDate").item(0).getFirstChild().getNodeValue());
                } else {
                    stkCompFinaciallist.add("");
                }

                if (eElement.getElementsByTagName("PeriodEndingDate").getLength() > 0) {
                    stkCompFinaciallist.add(eElement.getElementsByTagName("PeriodEndingDate").item(0).getFirstChild().getNodeValue());
                    out.println("PeriodEndingDate : " + eElement.getElementsByTagName("PeriodEndingDate").item(0).getFirstChild().getNodeValue());



                } else {
                    stkCompFinaciallist.add("");
                }

                if (eElement.getElementsByTagName("FileDate").getLength() > 0) {
                    stkCompFinaciallist.add(eElement.getElementsByTagName("Start").item(0).getFirstChild().getNodeValue());
                    out.println("FileDate : " + eElement.getElementsByTagName("FileDate").item(0).getFirstChild().getNodeValue());
                } else {
                    stkCompFinaciallist.add("");
                }

以上编码可以获得节点值。 因为标签名称超过200个标签,所以我计划首先使用for循环获取标签名称以获取值。 而这个标签也有另一个目的。

请建议如何获取标签名称?我只需要标记名BalansheetEntity

谢谢和最诚挚的问候 沙龙

2 个答案:

答案 0 :(得分:1)

你可以在这里使用Xpath吗?如果您的XML结构始终包含标记 - BalansheetEntity,则可以使用XPAth获取BalansheetEntity标记的所有子节点。

最好使用XSD验证xml结构。

此致 阿都

答案 1 :(得分:0)

基本上,您有两种方法可以迭代所有元素:

<强> 1。使用递归(我认为最常见的方式):     public static void main(String [] args)抛出SAXException,IOException,         ParserConfigurationException,TransformerException {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
    .newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("document.xml"));
doSomething(document.getDocumentElement());
}

public static void doSomething(Node node) {
// do something with the current node instead of System.out
System.out.println(node.getNodeName());

NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node currentNode = nodeList.item(i);
    if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
        //calls this method for all the children which is Element
        doSomething(currentNode);
    }
}
}

<强> 2。使用带有*作为参数的getElementsByTagName()方法避免递归:

public static void main(String[] args) throws SAXException, IOException,
    ParserConfigurationException, TransformerException {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
        .newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("document.xml"));

NodeList nodeList = document.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        // do something with the current element
        System.out.println(node.getNodeName());
    }
}
}

我认为这些方法都很有效。 希望这有帮助