File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
package com.munjaon.client.util;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
public class XmlUtil {
// private static Document getDOMParsedDocument(final String fileName) {
// Document document = null;
// try {
//
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// //If want to make namespace aware.
// //factory.setNamespaceAware(true);
// DocumentBuilder documentBuilder = factory.newDocumentBuilder();
// document = documentBuilder.parse(new File("C:\\Docs\\JDS\\ITN\\MMS01Header.xml"));
//
// NodeList nodeList = document.getDocumentElement().getChildNodes();
// for (int i = 0; i < nodeList.getLength(); i++) {
// Node node = nodeList.item(i);
// if (node.getNodeType() == Node.ELEMENT_NODE) {
// Element elem = (Element) node;
// System.out.println("createDate : " + elem.getAttribute("createDate"));
// System.out.println("getTagName : " + elem.getTagName());
// System.out.println("getNodeName : " + elem.getNodeName());
// System.out.println("getTextContent : " + elem.getTextContent());
//// String createDate = elem.getElementsByTagName("createDate").item(0).getChildNodes().item(0).getNodeValue();
//// System.out.println("createDate : " + createDate);
//// String PopCounter = elem.getElementsByTagName("PopCounter").item(0).getChildNodes().item(0).getNodeValue();
//// System.out.println("PopCounter : " + PopCounter);
//// Double salary = Double.parseDouble(elem.getElementsByTagName("salary").item(0).getChildNodes().item(0).getNodeValue());
// }
// }
// }
// catch (IOException | SAXException | ParserConfigurationException e) {
// e.printStackTrace();
// }
// return document;
// }
private static Document getSaxParsedDocument(final String fileName) {
Document document = null;
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ " <catalog>\r\n"
+ " <book id=\"bk101\">"
+ " <author>Gambardella, Matthew</author> "
+ "<title>XML Developer's Guide</title>"
+ " <genre>Computer</genre>"
+ " <price>44.95</price> "
+ "<publish_date>2000-10-01</publish_date> "
+ "<description>An in-depth look at creating applications with XML.</description> "
+ "</book>"
+ " <book id=\"bk102\">"
+ " <author>Ralls, Kim</author>"
+ " <title>Midnight Rain</title>"
+ " <genre>Fantasy</genre>"
+ " <price>5.95</price>"
+ " <publish_date>2000-12-16</publish_date>"
+ " <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>"
+ " </book> \r\n"
+ "</catalog>\r\n";
try {
SAXBuilder sax = new SAXBuilder();
// String that contains XML
Document doc = (Document) sax.build(new File("C:\\Docs\\JDS\\ITN\\MMS01Header.xml"));
// org.jdom2.Document doc = sax.build(new StringReader(xml));
Element rootNode = doc.getRootElement();
List<Element> bookElements = rootNode.getChildren();
System.out.println("bookElements: " + bookElements);
for(Element bookElement : bookElements){
String name = bookElement.getName();
String value = bookElement.getValue();
System.out.println(name + " : " + value);
}
} catch (IOException | JDOMException e) {
e.printStackTrace();
}
return document;
}
private static void writeSimpleXml() throws JDOMException, IOException {
String xml = "<root><child id=\"100\">mkyong</child></root>";
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new StringReader(xml));
Document docFile = new Document();
Element rootElement = new Element("ReadQueue");
rootElement.addContent(new Element("createDate").setText("20240527"));
rootElement.addContent(new Element("PopCounter").setText(Integer.toString(0)));
docFile.setRootElement(rootElement);
// default in compact mode
// XMLOutputter xmlOutputter = new XMLOutputter();
// pretty print format
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
// output to console
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Docs\\JDS\\ITN\\file.xml");
xmlOutputter.output(docFile, fileOutputStream);
}
public static void main(String[] args) throws IOException, JDOMException {
// XmlUtil.getDOMParsedDocument("C:\\Docs\\JDS\\ITN\\MMS01Header.xml");
XmlUtil.getSaxParsedDocument("C:\\Docs\\JDS\\ITN\\MMS01Header.xml");
XmlUtil.writeSimpleXml();
}
}