简体   繁体   English

Java-使用可变标记名解析xml字符串?

[英]Java - parse xml string with variable tagnames?

I'm trying to parse an XML string, and the tagnames are variable; 我正在尝试解析XML字符串,并且标记名是可变的; I haven't seen any examples on how to pull the information out without knowing them. 我还没有看到有关如何在不知情的情况下提取信息的示例。 For example, I will always know the <response> and <data> tags below, but what falls inside/outside of them could be anything from <employee> to you name it. 例如,我将始终知道下面的<response><data>标记,但是从<employee>到您命名的内容,它们的内部/外部可能是任何东西。

    <?xml version="1.0" encoding="UTF-8"?>

     <response> 
        <generic>
           ....
        </generic>   
        <data>
             <employee>
                <name>Seagull</name>
                <id>3674</id>
                <age>34</age>
             </employee>
             <employee>
                <name>Robin</name>
                <id>3675</id>
                <age>25</age>
             </employee>
       </data>
   </response>

You could parse it into a generic dom object and traverse it. 您可以将其解析为通用的dom对象并遍历它。 For example, you could use dom4j to do this. 例如,您可以使用dom4j来执行此操作。

From the dom4j quick start guide: 从dom4j快速入门指南:

public void treeWalk(Document document) {
    treeWalk( document.getRootElement() );
}

public void treeWalk(Element element) {
    for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
        Node node = element.node(i);
        if ( node instanceof Element ) {
            treeWalk( (Element) node );
        }
        else {
            // do something....
        }
    }
}

public Document parse(URL url) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(url);
    return document;
}

I have seen similar situation in the projects. 我在项目中也看到过类似的情况。

If you are going to deal with large XMLs, you can use Stax or Sax parser to read the XML. 如果要处理大型XML,则可以使用Stax或Sax解析器读取XML。 On every step (like on reaching end element), enter the data into a Map or a dta structure of your choice, where you keep tag names as the key and value as value in the Map. 在每个步骤上(例如到达end元素时),将数据输入Map或您选择的dta结构中,在其中您将标记名称作为键,将值保留为Map中的值。 Finally once you have the parsing done, use this Map to figure out which object to build as finally you would have a proper entity representation of the information in the XML 最后,一旦解析完成,就可以使用此Map找出要构建的对象,最后,您将拥有XML中信息的适当实体表示形式

If XML is small,use DOM and directly build the entity object by reading the specific tag (like employee> or use XPATh to where you expect the tag to be present, giving you hint of the entity. Build that object directly by reading the specific information from the XML. 如果XML很小,请使用DOM并通过读取特定标签直接构建实体对象(例如employee>或使用XPATh到您期望标签存在的位置,从而向您提供实体提示。通过读取特定对象直接构建该对象XML中的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM