我有一个XML文档,我需要将其转换为Excel工作表,以便数据更易于呈现,而且我还可以向其中添加宏。
XML Document非常复杂,它实际上是一个使用XStream转换为XML的Java Application Object。
我需要动态解析,其中标签应该是列名,属性应该是列值。
如果有人能建议我该怎么做呢,那将会有很大的帮助?
我尝试过使用Apache POI,但我无法动态选择XML元素并逐个迭代,并且根据标记名称选择值无法解决我的目的,因为文档的XML结构会不断变化。
提前致谢。
答案 0 :(得分:0)
我认为Apache POI是您的最佳选择,但可能与某些XML解析算法相结合。也许您可以尝试首先将所有可能的xml值存储在某个2维数组或列表中。在那之后你可以迭代它然后利用apache POI。
String[][] array = .. //parsed xml
for(String[] row : array){
createCell();
for(String cell : row){
createRow();
}
}
答案 1 :(得分:0)
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