簡體   English   中英

從Excel工作表生成XML輸出

[英]Xml Output generation from excel sheet

我正在嘗試將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數據(具有值和null)的ArrayList並將其轉換為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>

我該怎么辦?

要使用“ 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解析器的信息。 如何用Java創建XML文件-(DOM分析器)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM