开发者

Convert XML document to an Excel Sheet programmatically through Java

I have an XML Document and I need to convert it into an Excel Sheet so that the data is more presentable and also I would be able to add Macros to it.

The XML Document is pretty complex and it is actually a Java Application Object that has been converted to XML using XStream.

I need to do the parsing dynamically wherein the tags should be the column name and the attributes should be column value.

It would be of great 开发者_如何学Gohelp if someone could suggest how should I go about it ?

I have tried using Apache POI but I am not being able to pick the XML Elements dynamically and iterating one by one and picking the values based on tag names would not solve my purpose as the XML Structure of the Document would keep changing.

Thanks in advance.


I think Apache POI is your best choice but probably in conjuction with some XML parsing algorithm. Maybe you could try to first store all possible xml values in some 2 dimentional array or list. After that you could iterate on that and utilize apache POI then.

String[][] array = .. //parsed xml

for(String[] row : array){
      createCell();  
      for(String cell : row){
         createRow();
      }
}


I would suggest a combination of POI(as it can read the latest version of Excel)JAXB and XSD 

 1.First step would be to define a Schema Definition file(XSD) and define all the element names(classes) and its references(class attributes).

For example Imagine your XML like this:

<human>
   <man1>
     <age>
     </age>
   </man1>
   <man2>
     <age>
     </age>
   </man2>
      .
      .
 </human>

 2.Imagine each man1,man2 nodes as objects of a class called man so define a man element name in your XSD and define its elements.  
 3. After that generate the classes from that xsd, do a maven clean install if you are using Maven.
 4. Unmarshall the xml to objects of the generated classes.
 5. Read each of the object in a loop and using POI open a new Excel file and insert the read data into the Excel file 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜