관리 메뉴

nkdk의 세상

5장 DOM에 대하여 전체적으로 본문

My Programing/XML

5장 DOM에 대하여 전체적으로

nkdk 2008. 3. 8. 02:07
일단 복사 하는 방법부터 dom copy 방법
select 하여 삭제하는 방법
delete돔 하는방법
insert돔 하는 방법입니다 나갑니다.
선택한 값 중에 해당되는 것만 지우는 것
selectdom3는 해당되는 속성을 받아 오는 것입니다.

import java.io.*;

public class JavaCopy {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];
String arg1 = args[1];
try {
BufferedReader reader = new BufferedReader(new FileReader(arg0)); // arg에 0번째 파일을 읽음
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(arg1)));

String line;
while((line = reader.readLine()) != null) {
writer.println(line);
}
reader.close();
writer.close();

} catch (Exception e) {

}
}
}



import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;

public class InsertDom {
public static void main(String[] args) throws Exception{
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];
String arg1 = args[1];
// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서의 출발점 필요
Element root = doc.getDocumentElement();
// child로 이동 후 요소를 추가
addData(root);

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(arg1));
xdoc.write(bw, "EUC-KR");
}
public static void addData(Node n) { // element는 node의 일종이기 때문이다.
for(Node ch=n.getFirstChild();ch != null;ch=ch.getNextSibling()) {
if(ch.getNodeType() == Node.ELEMENT_NODE) {
if(ch.getNodeName().equals("student")) {
Document doc = ch.getOwnerDocument(); // 노드의 문서를 얹음(획득)
Element el = doc.createElement("addr");
Text txt = doc.createTextNode("서울시"); // 해당 엘리먼트에 텍스트를 부여함
ch.appendChild(el); // 자식노드를 추가 한다.
el.appendChild(txt);
}
addData(ch); // addData 하는 것이다.
}
}
}

}


import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;


public class DomCopy {

public static void main(String[] args) {
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];
String arg1 = args[1];
// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
try {
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(arg1));
xdoc.write(bw, "EUC-KR");
} catch (Exception e) {
System.out.println("dom"+e);
} // 돔의 준비가 완료
}

}


import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;

public class DeleteDom {
public static void main(String[] args) throws Exception{
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];

// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서의 출발점 필요
Element root = doc.getDocumentElement();
// child로 이동 후 요소를 추가
delData(root);

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
xdoc.write(bw, "EUC-KR");
}
public static void delData(Node n) { // element는 node의 일종이기 때문이다.
for(Node ch=n.getFirstChild();ch != null;ch=ch.getNextSibling()) {
if(ch.getNodeType() == Node.ELEMENT_NODE) {
if(ch.getNodeName().equals("age")) {
Node no = ch.getParentNode();
no.removeChild(ch); // 노드를 삭제 한다.

}
delData(ch); // addData 하는 것이다.
}
}
}
}

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;

public class SelectDom {
public static void main(String[] args) throws Exception{
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];

// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서를 새로 작성
Document doc2 = docB.newDocument();

// root 요소 추가
Element root = doc2.createElement("students");
doc2.appendChild(root);

//요소 추출, 노드 목록 획득
NodeList lst = doc.getElementsByTagName("name");
for(int i=0;i<lst.getLength();i++) {
Node n = lst.item(i);
for(Node ch=n.getFirstChild();ch!=null; ch=ch.getNextSibling()){
Element elm = doc2.createElement("name");
Text txt = doc2.createTextNode(ch.getNodeValue());
elm.appendChild(txt);
root.appendChild(elm);
}
}

delData(root);

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc2; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
xdoc.write(bw, "EUC-KR");
}
public static void delData(Node n) { // element는 node의 일종이기 때문이다.
for(Node ch=n.getFirstChild();ch != null;ch=ch.getNextSibling()) {
if(ch.getNodeType() == Node.ELEMENT_NODE) {
if(ch.getNodeName().equals("age")) {
Node no = ch.getParentNode();
no.removeChild(ch); // 노드를 삭제 한다.

}
delData(ch); // addData 하는 것이다.
}
}
}
}


import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;

public class SelectDom2 {
public static void main(String[] args) throws Exception{
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];

// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서를 새로 작성
Document doc2 = docB.newDocument();

// root 요소 추가
Element root = doc2.createElement("students");
doc2.appendChild(root);

//요소 추출, 노드 목록 획득
NodeList lst = doc.getElementsByTagName("name");
for(int i=0;i<lst.getLength();i++) {
Node n = lst.item(i);
for(Node ch=n.getFirstChild();ch!=null; ch=ch.getNextSibling()){
if(ch.getNodeValue().indexOf("한") != -1) {
Element elm = doc2.createElement("name");
Text txt = doc2.createTextNode(ch.getNodeValue());
elm.appendChild(txt);
root.appendChild(elm);
}
}
}

delData(root);

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc2; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
xdoc.write(bw, "EUC-KR");
}
public static void delData(Node n) { // element는 node의 일종이기 때문이다.
for(Node ch=n.getFirstChild();ch != null;ch=ch.getNextSibling()) {
if(ch.getNodeType() == Node.ELEMENT_NODE) {
if(ch.getNodeName().equals("age")) {
Node no = ch.getParentNode();
no.removeChild(ch); // 노드를 삭제 한다.

}
delData(ch); // addData 하는 것이다.
}
}
}
}

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;

public class SelectDom3 {
public static void main(String[] args) throws Exception{
if(args.length != 2) {
System.out.println("파일명 오류");
System.exit(-1);
}
String arg0 = args[0];

// DOM 준비
DocumentBuilderFactory docF = DocumentBuilderFactory.newInstance(); // 객체 생성됨
DocumentBuilder docB = docF.newDocumentBuilder();

// 문서 읽기
Document doc = docB.parse(new FileInputStream(arg0));

// 문서를 새로 작성
Document doc2 = docB.newDocument();

// root 요소 추가
Element root = doc2.createElement("students");
doc2.appendChild(root);

//요소 추출, 노드 목록 획득
NodeList lst = doc.getElementsByTagName("student");
for(int i=0;i<lst.getLength();i++) {
Node n = lst.item(i);
NamedNodeMap att = n.getAttributes();
Node a = att.getNamedItem("s_no");

Element elm = doc2.createElement("s_no");
Text txt = doc2.createTextNode(a.getNodeValue());
elm.appendChild(txt);
root.appendChild(elm);
}

delData(root);

// 문서 써 넣기
XmlDocument xdoc = (XmlDocument)doc2; // 실제로 여기서 적용
BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
xdoc.write(bw, "EUC-KR");
}
public static void delData(Node n) { // element는 node의 일종이기 때문이다.
for(Node ch=n.getFirstChild();ch != null;ch=ch.getNextSibling()) {
if(ch.getNodeType() == Node.ELEMENT_NODE) {
if(ch.getNodeName().equals("age")) {
Node no = ch.getParentNode();
no.removeChild(ch); // 노드를 삭제 한다.

}
delData(ch); // addData 하는 것이다.
}
}
}
}