2011-02-02 4 views
0

XML 문서가 있고 Excel 시트로 변환해야 데이터를보다 쉽게 ​​표시 할 수 있으며 매크로를 매크로에 추가 할 수 있습니다.Java를 통해 프로그래밍 방식으로 XML 시트를 Excel 시트로 변환

XML 문서는 매우 복잡하며 사실 XStream을 사용하여 XML로 변환 된 Java Application Object입니다.

동적으로 구문 분석을해야합니다. 여기서 태그는 열 이름이어야하며 속성은 열 값이어야합니다.

누군가 내가 어떻게해야하는지 제안 할 수 있다면 큰 도움이 될 것입니다.

아파치 POI를 사용해 보았지만 XML 요소를 동적으로 선택하지 않고 하나씩 반복하고 태그 이름을 기준으로 값을 선택하면 문서의 XML 구조가 계속 변경 될 수 있으므로 내 용도는 해결되지 않습니다. .

미리 감사드립니다.

답변

0

아파치 POI가 최선의 선택이지만 일부 XML 구문 분석 알고리즘과 결합 된 것 같습니다. 아마도 가능한 모든 XML 값을 2 차원 어레이 또는리스트에 저장하려고 시도 할 수 있습니다. 그 후에 당신은 그것을 반복하고 아파치 POI를 사용할 수 있습니다.

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

for(String[] row : array){ 
     createCell(); 
     for(String cell : row){ 
     createRow(); 
     } 
} 
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 
관련 문제