如何在Java中创建具有特定结构的XML文件[重复]
Posted
技术标签:
【中文标题】如何在Java中创建具有特定结构的XML文件[重复]【英文标题】:How to create XML file with specific structure in Java [duplicate] 【发布时间】:2014-06-24 13:44:29 【问题描述】:我想使用Java
创建XML
文件。
我的XML
文件结构:
<?xml version="1.0" encoding="UTF-8"?>
<CONFIGURATION>
<BROWSER>chrome</BROWSER>
<BASE>http:fut</BASE>
<ENVIRONMENT>abcd</ENVIRONMENT>
<USER>john</USER>
<PASSWORD>abcd123</PASSWORD>
<ORGANIZATION>Tim</ORGANIZATION>
<EMPLOYEE>
<EMP_NAME>Anhorn, Irene</EMP_NAME>
<ACT_DATE>20131201</ACT_DATE>
<DATE_IN>20131201</DATE_IN>
<CLOCK_IN>0800</CLOCK_IN>
<DATE_OUT>20131201</DATE_OUT>
<CLOCK_OUT>1600</CLOCK_OUT>
</EMPLOYEE>
<EMPLOYEE>
<EMP_NAME>Arlegui, Karen Jay</EMP_NAME>
<ACT_DATE>20131201</ACT_DATE>
<DATE_IN>20131201</DATE_IN>
<CLOCK_IN>1600</CLOCK_IN>
<DATE_OUT>20131202</DATE_OUT>
<CLOCK_OUT>0000</CLOCK_OUT>
</EMPLOYEE>
</CONFIGURATION>
【问题讨论】:
您尝试过任何代码吗?检查mkyong.com/java/how-to-create-xml-file-in-java-dom 我试过了,但是没用:( 【参考方案1】:您可以在 Java 中使用 JDOM 库。 将您的标签定义为Element 对象,使用Document Class 记录您的元素,并使用SAXBuilder 构建您的xml 文件。试试这个例子:
//Root Element
Element root=new Element("CONFIGURATION");
Document doc=new Document();
//Element 1
Element child1=new Element("BROWSER");
//Element 1 Content
child1.addContent("chrome");
//Element 2
Element child2=new Element("BASE");
//Element 2 Content
child2.addContent("http:fut");
//Element 3
Element child3=new Element("EMPLOYEE");
//Element 3 --> In this case this element has another element with Content
child3.addContent(new Element("EMP_NAME").addContent("Anhorn, Irene"));
//Add it in the root Element
root.addContent(child1);
root.addContent(child2);
root.addContent(child3);
//Define root element like root
doc.setRootElement(root);
//Create the XML
XMLOutputter outter=new XMLOutputter();
outter.setFormat(Format.getPrettyFormat());
outter.output(doc, new FileWriter(new File("myxml.xml")));
【讨论】:
【参考方案2】:public static void main(String[] args)
try
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("CONFIGURATION");
doc.appendChild(rootElement);
Element browser = doc.createElement("BROWSER");
browser.appendChild(doc.createTextNode("chrome"));
rootElement.appendChild(browser);
Element base = doc.createElement("BASE");
base.appendChild(doc.createTextNode("http:fut"));
rootElement.appendChild(base);
Element employee = doc.createElement("EMPLOYEE");
rootElement.appendChild(employee);
Element empName = doc.createElement("EMP_NAME");
empName.appendChild(doc.createTextNode("Anhorn, Irene"));
employee.appendChild(empName);
Element actDate = doc.createElement("ACT_DATE");
actDate.appendChild(doc.createTextNode("20131201"));
employee.appendChild(actDate);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/Users/myXml/ScoreDetail.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
catch (ParserConfigurationException pce)
pce.printStackTrace();
catch (TransformerException tfe)
tfe.printStackTrace();
XML 中的值是硬编码的。
【讨论】:
这是用JDOM库解析的吗? 我正在使用 DOM Parser lib 来解析一个 xml。【参考方案3】:使用 JAXB: http://www.mkyong.com/java/jaxb-hello-world-example/
package com.mkyong.core;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer
String name;
int age;
int id;
public String getName()
return name;
@XmlElement
public void setName(String name)
this.name = name;
public int getAge()
return age;
@XmlElement
public void setAge(int age)
this.age = age;
public int getId()
return id;
@XmlAttribute
public void setId(int id)
this.id = id;
package com.mkyong.core;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class JAXBExample
public static void main(String[] args)
Customer customer = new Customer();
customer.setId(100);
customer.setName("mkyong");
customer.setAge(29);
try
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
catch (JAXBException e)
e.printStackTrace();
【讨论】:
可能是我遇到的最有用的hello world。【参考方案4】:不需要任何外部库,JRE 系统库提供您所需的一切。
我推断你有一个 org.w3c.dom.Document
对象你想写入一个文件
为此,您可以使用javax.xml.transform.Transformer
:
import org.w3c.dom.Document
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class XMLWriter
public static void writeDocumentToFile(Document document, File file)
// Make a transformer factory to create the Transformer
TransformerFactory tFactory = TransformerFactory.newInstance();
// Make the Transformer
Transformer transformer = tFactory.newTransformer();
// Mark the document as a DOM (XML) source
DOMSource source = new DOMSource(document);
// Say where we want the XML to go
StreamResult result = new StreamResult(file);
// Write the XML to file
transformer.transform(source, result);
来源:http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html
【讨论】:
以上是关于如何在Java中创建具有特定结构的XML文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章