java:写入xml文件
在java中,我需要创建如下所示的xml文件:java:写入xml文件,java,xml,xsd,Java,Xml,Xsd,在java中,我需要创建如下所示的xml文件: <?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" exter
<?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>
200801
MIG5033028
MIG5033028
我在谷歌上找到了一些东西,但不符合我的需要。由于我是java新手,我不知道如何使它适应我的需要
谢谢你的帮助。不久前我遇到了类似的问题,我使用的网站让我很好地理解了不同的编码方式 为您提供了许多不同的xml编码方式:字符串、DOM、SAX
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
//Writing the string to a file
OutputStream outputStream;
byte buf[] = xmlString.getBytes();
outputStream = new FileOutputStream(file);
for (byte element : buf) {
outputStream.write(element);
}
outputStream.close();
buf = null;
不久前我遇到了一个类似的问题,我使用的网站让我很好地理解了不同的编码方式 为您提供了许多不同的xml编码方式:字符串、DOM、SAX
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
//Writing the string to a file
OutputStream outputStream;
byte buf[] = xmlString.getBytes();
outputStream = new FileOutputStream(file);
for (byte element : buf) {
outputStream.write(element);
}
outputStream.close();
buf = null;
我建议您改用JAXB。创建NikuDataBus、Header、CustomInformation等类。将它们标记为@XmlEntity。创建和填充对象
NikyDataBus dataBus = new NikuDataBus();
dataBus.setHeader(....)
//etc, etc....
File f = new File("mydata.xml");
Marshaller m = JAXBContext.newInstance(NikuDataBus.class, Header.class, CustomInformation.class ).createMarshaller().marshal(dataBus, f)
我建议您改用JAXB。创建NikuDataBus、Header、CustomInformation等类。将它们标记为@XmlEntity。创建和填充对象
NikyDataBus dataBus = new NikuDataBus();
dataBus.setHeader(....)
//etc, etc....
File f = new File("mydata.xml");
Marshaller m = JAXBContext.newInstance(NikuDataBus.class, Header.class, CustomInformation.class ).createMarshaller().marshal(dataBus, f)
我在这方面有很好的经验。您只需创建对象并用所需的任何数据填充它们,最后只需
xstream.toXML(对象)
获取xml字符串。我在这方面有很好的经验。您只需创建对象并用所需的任何数据填充它们,最后只需xstream.toXML(对象)
获取xml字符串。看起来您的xml基于xml模式(。/xsd/nikuxog_customObjectInstance.xsd
)。如果是这种情况,您可以使用。给定XML模式,XMLBean将生成一组Java类(或jar文件),您可以使用这些类以编程方式创建XML
优点是XML与XML模式兼容。我发现这种方法在过去很有用,而且我对XML bean有很好的经验。看起来您的XML基于XML模式(
。/xsd/nikuxog_customObjectInstance.xsd
)。如果是这种情况,您可以使用。给定XML模式,XMLBean将生成一组Java类(或jar文件),您可以使用这些类以编程方式创建XML
优点是XML与XML模式兼容。我发现这种方法在过去很有用,而且我对XML bean有很好的经验。我这样解决了它。这是一个看起来很糟糕的代码,但它对我来说很有效,可能是复制和粘贴导致了一些错误
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("");
}
}
公共类XcelReader{
私人无效setHenAllocKeyHeader(StringBuilder sb){
sb.append(“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”);
}
私有void setHenAllocKeyBottom(StringBuilder sb){
sb.append(“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”);
}
受保护的void jobRun()引发异常{
StringBuilder sb=新的StringBuilder();
塞森纳洛基领导(sb);
字符串prolog=sb.toString();
sb=新的StringBuilder();
setHenAllocKeyBottom(sb);
字符串epilog=sb.toString();
FileOutputStream fos=新的FileOutputStream(“c:\\test\\osem.xml”);
OutputStreamWriter osw=新的OutputStreamWriter(fos,Charset.forName(“UTF-8”);
写(prolog);
写(尾声);
osw.flush();
osw.close();
}
公共静态void main(字符串[]args){
试一试{
job.jobRun();
}捕获(例外e)
{
System.out.println(“”);
}
}
我这样解决了它。这是一个外观不好的代码,但它对我来说是有效的,可能是复制和粘贴导致了一些错误
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("");
}
}
公共类XcelReader{
私人无效setHenAllocKeyHeader(StringBuilder sb){
sb.append(“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”);
}
私有void setHenAllocKeyBottom(StringBuilder sb){
sb.append(“\r\n”
+“\r\n”
+“\r\n”
+“\r\n”);
}
受保护的void jobRun()引发异常{
StringBuilder sb=新的StringBuilder();
塞森纳洛基领导(sb);
字符串prolog=sb.toString();
sb=新的StringBuilder();
setHenAllocKeyBottom(sb);
字符串epilog=sb.toString();
FileOutputStream fos=新的FileOutputStream(“c:\\test\\osem.xml”);
OutputStreamWriter osw=新的OutputStreamWriter(fos,Charset.forName(“UTF-8”);
写(prolog);
写(尾声);
osw.flush();
osw.close();
}
公共静态void main(字符串[]args){
试一试{
job.jobRun();
}捕获(例外e)
{
System.out.println(“”);
}
}
您可以使用GitHub提供的Scilca XML Progression软件包
Node rootNode = Node.constructNode(<NikuDataBusxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_customObjectInstance.xsd">");
rootNode.addChildNode(Node.constructNode("<Header action="write" externalSource="NIKU" objectType="customObjectInstance" version="8.1.0.4247"/>"));
String customObj = "<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>"
+ "</CustomInformation>"
+ "<OBSAssocs/><Security/>"
+ "</instance>"
+ "</customObjectInstances>"
// Now build to customObject element using a VirtualXML Iterator
XMLIterator xi = new VirtualXML.XMLIterator(customObj);
Node customO = Node.readFromFile(xi);
rootNode.addChildNode(customO);
Document XmlDocument = new Document(rootNode);
XmlDocument.addXmlDeclaration(1.0, "UTF-8", null);
XMLWriter xw = XmlDocument.getWriter();
xw.write("D:/file.txt");
Node rootNode=Node.constructNode(“);
rootNode.addChildNode(Node.constructNode(“”));
字符串customObj=“”
+ ""
+ ""
+ "200801"
+“MIG5033028”
+“MIG5033028”
+ ""
+ ""
+ ""
+ ""
//现在使用VirtualXML迭代器构建customObject元素
席明器X=新的虚拟XML XMLIterator(Cuto Objo);
Node customO=Node.readFromFile(xi);
rootNode.addChildNode(customO);
Document XmlDocument=新文档(rootNode);
addXmlDeclaration(1.0,“UTF-8”,空);
XMLWriter xw=XmlDocument.getWriter();
写入(“D:/file.txt”);
您可以使用GitHub提供的Scilca XML Progression软件包
Node rootNode = Node.constructNode(<NikuDataBusxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/nikuxog_customObjectInstance.xsd">");
rootNode.addChildNode(Node.constructNode("<Header action="write" externalSource="NIKU" objectType="customObjectInstance" version="8.1.0.4247"/>"));
String customObj = "<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>"
+ "</CustomInformation>"
+ "<OBSAssocs/><Security/>"
+ "</instance>"
+ "</customObjectInstances>"
// Now build to customObject element using a VirtualXML Iterator
XMLIterator xi = new VirtualXML.XMLIterator(customObj);
Node customO = Node.readFromFile(xi);
rootNode.addChildNode(customO);
Document XmlDocument = new Document(rootNode);
XmlDocument.addXmlDeclaration(1.0, "UTF-8", null);
XMLWriter xw = XmlDocument.getWriter();
xw.write("D:/file.txt");
Node rootNode=Node.constructNode(“);
rootNode.addChildNode(Node.constructNode(“”));
字符串customObj=“”
+ ""
+ ""
+ "200801"
+“MIG5033028”
+“MIG5033028”
+ ""
+ ""
+ ""