如何在java中以编程方式创建xml结构

时间:2013-12-12 07:23:30

标签: java xml xml-parsing

我想在java中以编程方式创建一个xml结构

结构就像这样

<?xml version="1.0" encoding="UTF-8"?>
 <System token="com.test.dummy">
     <Parameter token="xyz">
        <Value>4</Value>
     </Parameter>
  </System>

我必须创建一个临时文件,然后将此数据插入文件中。 我是这个领域的新手,有人可以帮助我。

2 个答案:

答案 0 :(得分:7)

使用DOMParser最佳方式在java中编写xml

http://docs.oracle.com/cd/B13789_01/appdev.101/b12024/oracle/xml/parser/v2/DOMParser.html

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLFile {

public static void main(String argv[]) {

  try {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);

    // staff elements
    Element staff = doc.createElement("Staff");
    rootElement.appendChild(staff);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue("1");
    staff.setAttributeNode(attr);

    // shorten way
    // staff.setAttribute("id", "1");

    // firstname elements
    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("yong"));
    staff.appendChild(firstname);

    // lastname elements
    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("mook kim"));
    staff.appendChild(lastname);

    // nickname elements
    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("mkyong"));
    staff.appendChild(nickname);

    // salary elements
    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("100000"));
    staff.appendChild(salary);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

    // Output to console for testing
    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

    System.out.println("File saved!");

  } catch (ParserConfigurationException pce) {
    pce.printStackTrace();
  } catch (TransformerException tfe) {
    tfe.printStackTrace();
  }
}
}

请参阅此http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

答案 1 :(得分:2)

您也可以使用StringBuilder来构建xml结构。 StringBuilder重量轻,但不知道XML,所以根本不验证结构。但由于其简单性,它的价值在于它。

StringBuilder xmlBuilder = new StringBuilder("<?xml version="1.0" encoding="UTF-8"?>");
   xmlBuilder.append("<System token=\"com.test.dummy\">");
   xmlBuilder.append("<Parameter token=\"xyz\">").append("<Value>4</Value>");
   xmlBuilder.append("</Parameter>");
   xmlBuilder.append("</System>");
   xmlBuilder.toString();

您还可以让元素接受动态值。 确保您的xml文档结构正确