Java 怎么使用javax.xml.stream.xmlstreamwriter类编写XML文档

2019年7月29日21:01:47Java 怎么使用javax.xml.stream.xmlstreamwriter类编写XML文档已关闭评论 550 2592字阅读8分38秒

1.简介

Java 怎么使用javax.xml.stream.xmlstreamwriter类编写XML文档?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class Main {

  public void run(String outputFile) throws FileNotFoundException,
      XMLStreamException, IOException {
    List<Patient> patients = new ArrayList<>();
    Patient p1 = new Patient();
    Patient p2 = new Patient();
    Patient p3 = new Patient();
    p1.setId(BigInteger.valueOf(1));
    p1.setName("Ana");
    p1.setDiagnosis("Common Cold");
    p2.setId(BigInteger.valueOf(2));
    p2.setName("Mary");
    p2.setDiagnosis("Broken Ankle");
    p3.setId(BigInteger.valueOf(3));
    p3.setName("Brown");
    p3.setDiagnosis("Food Allergy");
    patients.add(p1);
    patients.add(p2);
    patients.add(p3);
    XMLOutputFactory factory = XMLOutputFactory.newFactory();
    try (FileOutputStream fos = new FileOutputStream(outputFile)) {
      XMLStreamWriter writer = factory.createXMLStreamWriter(fos, "UTF-8");
      writer.writeStartDocument();
      writer.writeCharacters("\n");
      writer.writeStartElement("patients");
      writer.writeCharacters("\n");
      for (Patient p : patients) {
        writer.writeCharacters("\t");
        writer.writeStartElement("patient");
        writer.writeAttribute("id", String.valueOf(p.getId()));
        writer.writeCharacters("\n\t\t");
        writer.writeStartElement("name");
        writer.writeCharacters(p.getName());
        writer.writeEndElement();
        writer.writeCharacters("\n\t\t");
        writer.writeStartElement("diagnosis");
        writer.writeCharacters(p.getDiagnosis());
        writer.writeEndElement();
        writer.writeCharacters("\n\t");
        writer.writeEndElement();
        writer.writeCharacters("\n");
      }
      writer.writeEndElement();
      writer.writeEndDocument();
      writer.close();
    }

  }

  public static void main(String[] args) {
    String fileName = null;
    if (args.length != 1) {
      System.out.printf("Usage: java DocWriter <outputXmlFile>\n");
      fileName = "patients.xml";
    } else {
      fileName = args[0];
    }
    Main app = new Main();
    try {
      app.run(fileName);
    } catch (FileNotFoundException | XMLStreamException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

class Patient {

  protected String name;

  protected String diagnosis;

  protected BigInteger id;

  
  public String getName() {
    return name;
  }

  
  public void setName(String value) {
    this.name = value;
  }

  
  public String getDiagnosis() {
    return diagnosis;
  }

  
  public void setDiagnosis(String value) {
    this.diagnosis = value;
  }

  
  public BigInteger getId() {
    return id;
  }

  
  public void setId(BigInteger value) {
    this.id = value;
  }

}

 
继续阅读
编程十万个怎么办