我见过许多程序,可以使用带有指定字段的Java创建XML文档。我还没遇到过用户可以决定rootElement和childEelement的名字的地方。有谁知道怎么回事?
答案 0 :(得分:3)
使用XStream
Java代码
XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
输出XML
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>
答案 1 :(得分:1)
查看此示例,您可以使用JDOM解析器在示例末尾获取xml。
Creating an XML document using Java
创建XML文件的JDOM示例。
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class WriteXMLFile {
public static void main(String[] args) {
try {
Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);
Element staff = new Element("staff");
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname").setText("yong"));
staff.addContent(new Element("lastname").setText("mook kim"));
staff.addContent(new Element("nickname").setText("mkyong"));
staff.addContent(new Element("salary").setText("199999"));
doc.getRootElement().addContent(staff);
Element staff2 = new Element("staff");
staff2.setAttribute(new Attribute("id", "2"));
staff2.addContent(new Element("firstname").setText("low"));
staff2.addContent(new Element("lastname").setText("yin fong"));
staff2.addContent(new Element("nickname").setText("fong fong"));
staff2.addContent(new Element("salary").setText("188888"));
doc.getRootElement().addContent(staff2);
// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\file.xml"));
System.out.println("File Saved!");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
创建的XML文件位于下方。
<?xml version="1.0" encoding="UTF-8"?>
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>199999</salary>
</staff>
<staff id="2">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>188888</salary>
</staff>
</company>
谢谢..
答案 2 :(得分:0)
以下是如何执行此操作的示例。当然有更多的方法来实现它。这个是使用JAXB。 (Java XML绑定)
http://www.mkyong.com/java/jaxb-hello-world-example/
这是一个非常简单的例子。 JAXB很好,因为你使用标准Java注释你的pojos:
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SimpleXML {
String name;
int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static void main(String args[]) throws JAXBException {
File xmlFile = new File("C:/temp/test.xml");
SimpleXML xml = new SimpleXML();
xml.setId(4);
xml.setName("TestName");
JAXBContext context = JAXBContext.newInstance(SimpleXML.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Marshaller marshaller = context.createMarshaller();
//write to file
marshaller.marshal(xml, xmlFile);
//read from file
SimpleXML newXML=(SimpleXML)unmarshaller.unmarshal(xmlFile);
System.out.println(newXML.id);
System.out.println(newXML.name);
}
}
这个例子创建了一个SimpleXML实例,并将其写入文件中。该文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<simpleXML><id>4</id><name>TestName</name></simpleXML>
之后,在另一个SimpleXML实例中再次读入该文件。保留所有值。
JAXB为您提供了调整XML输出(标记名称,属性名称,数据转换器和内容)的大量可能性。