parse this xml file in java

This question already has an answer here:

  • Which is the best library for XML parsing in java [closed] 7 answers

  • public static void main(String[] args) throws MarshalException,
    ValidationException, ParserConfigurationException, SAXException,
    IOException {
    
        File fXmlFile = new File(
                "/home/Parsing/src/com/parsing/test.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
    
        doc.getDocumentElement().normalize();
    
        Node node = doc.getDocumentElement().getParentNode();
    
        System.out.println(doc.getDocumentElement().getNodeName());
    
        NodeList itemList = node.getChildNodes();
        for (int i = 0; i < itemList.getLength(); i++) {
    
            Node nNode = itemList.item(i);
    
            System.out.println("Current Element : " + nNode.getNodeName());
    
            getChildNode(nNode.getChildNodes());
    
        }
    }
    
    private static void getChildNode(NodeList childNodes) {   // This method is going to retrieve the child nodes
        // TODO Auto-generated method stub
        System.out.println(childNodes.getLength());
    
        for (int i = 1; i < childNodes.getLength(); i++) {
            Node cNode = childNodes.item(i);
    
            System.out.println(cNode.getNodeName());
            /**
             * This will get the attribute of the node 
             */
            if (cNode.hasAttributes()) {
    
                NamedNodeMap nodeMap = cNode.getAttributes();
                for (int f = 0; f < nodeMap.getLength(); f++) {
                    System.out.println("Att " + nodeMap.item(f).getNodeName()
                            + " " + nodeMap.item(f).getNodeValue());
                }
            }
    
            if (cNode.hasChildNodes()) {
    
                // For getting the value if node has more than 2 or atleast two childs
    
                if (cNode.getChildNodes().getLength() >= 2) {
    
                    getChildNode(cNode.getChildNodes());
                }
    
                // For getting the node has no childs and it contains text node value
    
                else if (cNode.getNodeType() == cNode.ELEMENT_NODE) {
    
                    Element ele = (Element) cNode;
                    System.out.println("t" + ele.getTextContent());
    
                }
            }
    
            i++;
        }
    }
    

    One way to go would be to do it manually, by evaluating the strings. Another would be to use one of all the available Java XML libraries... http://en.wikipedia.org/wiki/Java_API_for_XML_Processing


    The answer is specific for a parser type. If you are going to use DOM parser, probably this will be helpful. These are examples for the SAX parser.

    链接地址: http://www.djcxy.com/p/51712.html

    上一篇: 创建一个XSD来处理抽象类型

    下一篇: 用java解析这个xml文件