0

假设我们有以下xml

<books>
   <book>
      <id>...</id>  
      <title>...</title>
      <references>
         <book>
           <id>...</id>  
           <title>...</title>
         </book>
      </references>            
      <authors>
         <author>
           <id>...</id>
           <name>...</name>
            <written-books>
              <book>
              <id>...</id>  
              <title>...</title>
              </book>
            </written-books>
         </author>
      </authors>
   <book>
</books>

我现在要做的是使用布尔值来找出我们在哪些部分(书籍、参考文献、作者、书面书籍)。我在开始/结束元素事件上执行此操作,然后在字符事件上检查

if (qName.equals("id"){
   if (writtenBooks){
   }else if (authors){
   }else if (references){
   }else{
   }
}else if ("title")...

但它似乎并不太优雅。有更好的解决方案吗?

4

1 回答 1

1

According to my understanding what you have to do is read the XML and then process it. (Check the existence of the elements etc.) You can use Apache Axiom to read xml files and get the content easily. Then you can process it as you want.I will put a example code segment to get the content of the xml file using AXIOM.

    //read the xml file
    StAXOMBuilder builder = new StAXOMBuilder(xmlStreamReader);
    OMElement endPointElem = builder.getDocumentElement();


   // go though the xml elemetns and do whatever you want
    Iterator children = endPointElem.getChildElements();
    while (children.hasNext()) {
    ........................
    .......
    }

For more details This post will helpful to you

于 2013-11-12T05:27:31.570 回答