skaryu skaryu - 1 year ago 142
Java Question

java: write to xml file

in java I need to create xml file which look like this:

<?xml version="1.0" encoding="UTF-8"?>
<NikuDataBus xmlns:xsi="" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_customObjectInstance.xsd">
<Header action="write" externalSource="NIKU" objectType="customObjectInstance" version=""/>
<customObjectInstances objectCode="hen_allockey_p">
<instance instanceCode="MIG5033028" objectCode="hen_allockey_p"
parentInstanceCode="001260" parentObjectCode="project">
<ColumnValue name="hen_from">200801</ColumnValue>
<ColumnValue name="name">MIG5033028</ColumnValue>
<ColumnValue name="code">MIG5033028</ColumnValue>

I found something on google, but it didn't match to my needs. And as I am new with java, I don't know how to adapt it to my needs.

Thank you for your help.

Answer Source

I resolved it like this. It is bad looking code, but it works for me, maybe there are some errors caused by copy and paste

public class POIExcelReader {

private void setHenAllocKeyHeader(StringBuilder sb) {
    sb.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
            + "<NikuDataBus xmlns:xsi=\"\" xsi:noNamespaceSchemaLocation=\"../xsd/nikuxog_customObjectInstance.xsd\">\r\n"
            + "<Header action=\"write\" externalSource=\"NIKU\"objectType=\"customObjectInstance\" version=\"\"/>\r\n"
            + "<customObjectInstances objectCode=\"hen_allockey_p\">\r\n"
            + "<instance instanceCode=\"MIG5033028\" objectCode=\"hen_allockey_p\" parentInstanceCode=\"001260\" parentObjectCode=\"project\">\r\n");

private void setHenAllocKeyBottom (StringBuilder sb) {
    sb.append ("<OBSAssocs/>\r\n"
            + "</NikuDataBus>\r\n");

protected void jobRun() throws Exception {

    StringBuilder sb = new StringBuilder();
    String prolog = sb.toString();

    sb = new StringBuilder();
    String epilog = sb.toString();

    FileOutputStream fos = new FileOutputStream("c:\\test\\osem.xml");
    OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));

public static void main(String[] args){
    } catch (Exception e)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download