Uknown XML file into pojo

扶醉桌前 提交于 2019-12-02 10:19:35

In the hope that this helps you to understand your situation!

public static void dumpAllNodes( String path ) throws Exception {
    DocumentBuilder parser = 
      DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = parser.parse(new File(path));
    NodeList nodes = doc.getElementsByTagNameNS( "*", "*" );
    for( int i = 0; i < nodes.getLength(); i++ ){
        Node node = nodes.item( i );
        System.out.println( node.getNodeType() + " " + node.getNodeName() );
    }
}

The NodeList nodes contains all element nodes, in document order (opening tag). Thus, elements contained within elements will be in that list, all alike. To obtain the attributes of a node, call

NamedNodeMap map = node.getAttributes();

The text content of a node is available by

String text = node.getTextContent();

but be aware that calling this returns the text in all elements of a subtree.

OTOH, you may call

Element root = doc.getDocumentElement();

to obtain the root element and then descend the tree recursively by calling Element.getChildNodes() and process the nodes (Element, Attr, Text,...) one by one. Also, note that Node.getParentNode() returns the parent node, so you could construct the XPath for each node even from the flat list by repeating this call to the root.

It simply depends what you expect from the resulting data structure (what you call ArrayList). If, for instance, you create a generic element type (MyElement) containing one map for attributes and another one for child elements, the second map would have to be

Map<String,List<MyElement>> name2elements

to provide for repeated elements - which makes access to elements occurring only once a little awkward.

I hope that I have illustrated the problems of generic XML parsing, which is not a task where JAXB can help you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!