Uknown XML file into pojo

2019-03-04 08:41发布

问题:

Is there any way to take an unknown schema xml file and convert the values, tags, etc from the xml file into pojo? I looked at jaxb and it seems like the best way to use that is if you already know the xml schema. So basically, I want to be able to parse the tags from the xml put each into its own object maybe through the use of an arraylist. Did I just not fully understand what jaxb can or is there a better tool that can do this, or is it just too hard to implement?

回答1:

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.



标签: java xml jaxb pojo