将未知的XML文件导入pojo [英] Uknown XML file into pojo

查看:124
本文介绍了将未知的XML文件导入pojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取未知的架构xml文件并将值,标签等从xml文件转换为pojo?我看了jaxb,看起来最好的方法就是你已经知道了xml架构。所以基本上,我希望能够通过使用arraylist解析xml中的标记,将每个标记放入自己的对象中。我还没有完全理解jaxb可以或者有更好的工具可以做到这一点,还是实现起来太难了?

解决方案

希望这有助于您了解您的情况!

  public static void dumpAllNodes(String path)抛出异常{
DocumentBuilder解析器=
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());
}
}

NodeList节点包含所有元素节点,按文档顺序(打开标记)。因此,元素中包含的元素将在该列表中,都是相同的。要获取节点的属性,请调用

  NamedNodeMap map = node.getAttributes(); 

节点的文本内容可通过



获得

  String text = node.getTextContent(); 

请注意,调用它会返回子树的所有元素中的文本



OTOH,你可以致电

 元素root = doc .getDocumentElement(); 

获取根元素,然后通过调用元素递归地下降树。 getChildNodes()并逐个处理节点(Element,Attr,Text,...)。另请注意, Node.getParentNode()会返回父节点,因此您可以通过重复调用root来为平面列表构建每个节点的XPath。 / p>

它只取决于您对结果数据结构的期望(您称之为ArrayList)。例如,如果您创建一个包含一个属性映射的通用元素类型(MyElement),另一个用于子元素,则第二个映射必须是

 地图<字符串,列表< MyElement>> name2elements 

提供重复的元素 - 这使元素的访问只发生一次有点尴尬。



我希望我已经说明了通用XML解析的问题,这不是JAXB可以帮助你的任务。


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?

解决方案

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.

这篇关于将未知的XML文件导入pojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆