我正在尝试将excel文件转换为java中的xml文件。
try {
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("dataroot");
doc.appendChild(root);
Element Details = doc.createElement("DATA");
root.appendChild(Details);
for(int i=0; i<list.size()-2; i +=3 ) {
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
Details.appendChild(name);
Element id = doc.createElement("Empid");
id.appendChild(doc.createTextNode(String.valueOf(list.get(i+1))));
Details.appendChild(id);
Element ad = doc.createElement("Add");
ad.appendChild(doc.createTextNode(String.valueOf(list.get(i+2))));
Details.appendChild(ad);
Element mo = doc.createElement("Mobile");
mo.appendChild(doc.createTextNode(String.valueOf(list.get(i+3))));
Details.appendChild(mo);
}
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// format the XML nicely
aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
aTransformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
这是我读取excel数据的ArrayList(带值和null)并将其转换为xml的代码。
我的输出是:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<dataroot generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile>12333333</Mobile>
</DATA>
<DATA>
<Name>12333333</Name>
<Empid>Azam</Empid>
<Add>FBL321</Add>
<Mobile>Dhaka</Mobile>
</DATA>
<DATA>
<Name>Dhaka</Name>
<Empid>67778888</Empid>
<Add>Rony</Add>
<Mobile>Chandpur</Mobile>
</DATA>
<DATA>
<Name>Chandpur</Name>
<Empid>099776655</Empid>
<Add>Azam</Add>
<Mobile>9988</Mobile>
</DATA>
</dataroot>
但是我想要的xml输出是:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
<DATA>
<Name>AZAM</Name>
<Empid>FBL321</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
</dataroot>
我怎么做?
答案 0 :(得分:0)
要拥有“DATA”标签,您应将下面的行移到for语句中:
Element Details = doc.createElement("DATA");
root.appendChild(Details);
要在“dataroot”上添加一些属性,请使用以下代码:
root.setAttribute("generated", "2015-04-26T10:52:27");
在移动元素上,试试这个:
Element mo = doc.createElement("Mobile");
Element prop = doc.createElement("Prop");
prop.setAttribute("ID", "FAMILY");
prop.setAttribute("ValStr", "Mrs. YYYYYYYY");
Element prop2 = doc.createElement("Prop");
prop2.setAttribute("ID", "Personal");
prop2.setAttribute("ValStr", "Mrs. ZZZZZZZZ");
mo.appendChild(prop);
mo.appendChild(prop2);
尝试阅读本教程中的DOM解析器。 How to create XML file in Java – (DOM Parser)