Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[Java] 寫入及讀取 XML 檔案
@XmlRootElement(name = "centerInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class CenterInfo extends BaseXmlConfig {
@XmlAttribute(name = "Id")
private String id;
@XmlAttribute(name = "Name")
private String name;
@XmlAttribute(name = "status")
private String status;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
@XmlRootElement(name = "centerList")
@XmlAccessorType(XmlAccessType.FIELD)
public class CenterList extends BaseXmlConfig {
@XmlElement(name = "centerInfo")
private List<CenterInfo> centerInfo;
public List<CenterInfo> getCenterInfo() {
return centerInfo;
}
public void setCenterInfo(List<CenterInfo> centerInfo) {
this.centerInfo = centerInfo;
}
}
/**
* https://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
*/
@Test
public void testWriteXmlUseDocument() {
final String ROOT_ELEMENT = "centerList";
final String CHILDREN_ELEMENT = "centerInfo";
final String ID = "Id";
final String NAME = "Name";
final String STATUS = "status";
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root element
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement(ROOT_ELEMENT);
doc.appendChild(rootElement);
// centerInfo elements
Element centerInfo = doc.createElement(CHILDREN_ELEMENT);
rootElement.appendChild(centerInfo);
Element centerInfo2 = doc.createElement(CHILDREN_ELEMENT);
rootElement.appendChild(centerInfo2);
Attr attrId = doc.createAttribute(ID);
attrId.setValue("777");
centerInfo.setAttributeNode(attrId);
Attr attrName = doc.createAttribute(NAME);
attrName.setValue("測試");
centerInfo.setAttributeNode(attrName);
Attr attrStatus = doc.createAttribute(STATUS);
attrStatus.setValue("0");
centerInfo.setAttributeNode(attrStatus);
Attr attrId2 = doc.createAttribute(ID);
attrId2.setValue("6000");
centerInfo2.setAttributeNode(attrId2);
Attr attrName2 = doc.createAttribute(NAME);
attrName2.setValue("測試二");
centerInfo2.setAttributeNode(attrName2);
Attr attrStatus2 = doc.createAttribute(STATUS);
attrStatus2.setValue("1");
centerInfo2.setAttributeNode(attrStatus2);
// 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("test.xml"));
transformer.transform(source, result);
Assert.assertNotNull(result);
} catch (ParserConfigurationException | TransformerException e) {
e.printStackTrace();
}
}
private CenterList centerList;
private CenterInfo centerInfo;
private CenterInfo centerInfo2;
private List<CenterInfo> centerInfos;
@Before
public void setUp() {
prepareCenterConfig();
}
/**
* http://www.journaldev.com/1234/jaxb-example-tutorial
* http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
*/
@Test
public void testWriteXmlUseJaxb() {
centerList.setCenterInfo(centerInfos);
jaxbObjectToXML(centerList);
CenterList centerList1 = jaxbXMLToObject();
Assert.assertNotNull(centerList1);
}
private void jaxbObjectToXML(CenterList centerList) {
try {
JAXBContext context = JAXBContext.newInstance(CenterList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(centerList, new File("test.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
private CenterList jaxbXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(CenterList.class);
Unmarshaller un = context.createUnmarshaller();
CenterList centerList = (CenterList) un.unmarshal(new File("test.xml"));
return centerList;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
private void prepareCenterConfig() {
centerList = new CenterList();
centerInfo = new CenterInfo();
centerInfo.setId("777");
centerInfo.setName("測試");
centerInfo.setStatus("1");
centerInfo2 = new CenterInfo();
centerInfo2.setId("7778");
centerInfo2.setName("測試2");
centerInfo2.setStatus("0");
centerInfos = new ArrayList<>();
centerInfos.add(centerInfo);
centerInfos.add(centerInfo2);
centerList.setCenterInfo(centerInfos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment