일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Windows
- JavaScript
- 네트워크
- Mac
- xargs
- 줄바꿈 문자
- maVen
- netsh
- lsof
- grep
- Quartz
- Eclipse
- profile
- resource
- bash
- find
- Source
- vscode
- 단축키
- VirtualBox
- tomcat
- Windows 10
- IntelliJ
- context
- plugin
- GIT
- ssh
- web.xml
- port
- import
- Today
- Total
develog
XML DOM 본문
test.xml
--------------------------------------------------------------------------
<?xml version="1.0" encoding="EUC-KR" standalone="no" ?>
<books>
<book id="11">
<name>XML1</name>
<author>홍길동</author>
<price>100</price>
</book>
<book id="22">
<name>고길동</name>
<author>200</author>
<price>200</price>
</book>
<book id="33">
<name>김철수</name>
<author>300</author>
<price>300</price>
</book>
<book id="44">
<name>이영희</name>
<author>400</author>
<price>400</price>
</book>
</books>
XmlTest.java
--------------------------------------------------------------------------
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlTest {
public static void main(String[] args) throws Exception {
// createByDOM();
parseByDOM();
}
private static void createByDOM() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
Element staff = doc.createElement("staff");
rootElement.appendChild(staff);
Attr attr = doc.createAttribute("id");
attr.setValue("1");
staff.setAttributeNode(attr);
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("yong"));
staff.appendChild(firstname);
Element nickname = doc.createElement("nickname");
nickname.appendChild(doc.createTextNode("mkyoung"));
staff.appendChild(nickname);
Element salary = doc.createElement("salary");
salary.appendChild(doc.createTextNode("100"));
staff.appendChild(salary);
DOMSource xmlSource = new DOMSource(doc);
StreamResult outputTarget = new StreamResult(new File("output.xml"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(xmlSource, outputTarget);
}
private static void parseByDOM() throws Exception {
File file = new File("test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
if (doc.hasChildNodes()) {
printNode(doc.getChildNodes());
}
}
private static void printNode(NodeList nodeList) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
if (node.getChildNodes().getLength() == 1) {
System.out.println("\t" + node.getNodeName() + "=" + node.getTextContent());
} else {
System.out.println(node.getNodeName());
}
if (node.hasAttributes()) {
NamedNodeMap nodeMap = node.getAttributes();
for (int j = 0; j < nodeMap.getLength(); j++) {
Node attrNode = nodeMap.item(j);
System.out.println("\t" + "attr : " + attrNode.getNodeName() + "=" + attrNode.getNodeValue());
}
}
if (node.hasChildNodes()) {
printNode(node.getChildNodes());
}
}
}
}
'Dev > Java' 카테고리의 다른 글
dbcp test (0) | 2013.03.01 |
---|---|
variable arguments (0) | 2013.02.23 |
AES (0) | 2013.01.27 |
poi 대용량 엑셀 write (0) | 2013.01.08 |
Quartz job fire manually (0) | 2012.12.18 |