java:写入xml文件 [英] java: write to xml file
本文介绍了java:写入xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在java中我需要创建如下所示的xml文件:
in java I need to create xml file which look like this:
<?xml version="1.0" encoding="UTF-8"?>
<NikuDataBus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_customObjectInstance.xsd">
<Header action="write" externalSource="NIKU" objectType="customObjectInstance" version="8.1.0.4247"/>
<customObjectInstances objectCode="hen_allockey_p">
<instance instanceCode="MIG5033028" objectCode="hen_allockey_p"
parentInstanceCode="001260" parentObjectCode="project">
<CustomInformation>
<ColumnValue name="hen_from">200801</ColumnValue>
<ColumnValue name="name">MIG5033028</ColumnValue>
<ColumnValue name="code">MIG5033028</ColumnValue>
<OBSAssocs/>
<Security/>
</instance>
</customObjectInstances>
</NikuDataBus>
我在谷歌上找到了一些东西,但它不符合我的需求.由于我是 Java 新手,我不知道如何使其适应我的需求.
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.
感谢您的帮助.
推荐答案
我是这样解决的.看起来很糟糕的代码,但它对我有用,也许是复制和粘贴导致的一些错误
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=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"../xsd/nikuxog_customObjectInstance.xsd\">\r\n"
+ "<Header action=\"write\" externalSource=\"NIKU\"objectType=\"customObjectInstance\" version=\"8.1.0.4247\"/>\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"
+"<Security/>\r\n"
+"</customObjectInstances>\r\n"
+ "</NikuDataBus>\r\n");
}
protected void jobRun() throws Exception {
StringBuilder sb = new StringBuilder();
setHenAllocKeyHeader(sb);
String prolog = sb.toString();
sb = new StringBuilder();
setHenAllocKeyBottom(sb);
String epilog = sb.toString();
FileOutputStream fos = new FileOutputStream("c:\\test\\osem.xml");
OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
osw.write(prolog);
osw.write(epilog);
osw.flush();
osw.close();
}
public static void main(String[] args){
try{
job.jobRun();
} catch (Exception e)
{
System.out.println("");
}
}
这篇关于java:写入xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文