0

enter image description here

above is my XMl...

how to get the name of the element tag like ReportDate, PeriodEndingDate, FileDate and so on?

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("");
                }

Above Coding can get the Node Value. Cause the tag name is more than 200 tag, so i plan get the tag name first just using for loop to get the value. and this tag also have another purpose.

please kindly advice how to get the tag name? i only need the tag name of BalansheetEntity.

Thanks and best regards Sharon

assembler
  • 3,098
  • 12
  • 43
  • 84
Sharon Wong
  • 109
  • 1
  • 2
  • 14

1 Answers1

1

can you use Xpath here. if your XML structure always contain tag - BalansheetEntity you can use XPAth to get all child nodes of BalansheetEntity tag.

Also it is better to use XSD to validate xml structure.

Regards Abdul

Abdul Azeez
  • 807
  • 10
  • 18