String xmlStr = "<?xml version=\"1.0\" encoding=\"euc-kr\" ?>"; xmlStr += "<root>"; xmlStr += "<header>"; xmlStr += "<data>"; xmlStr += "<이름>ddakker</이름>"; xmlStr += "<날짜>2010-09-03</날짜>"; xmlStr += "</data>"; xmlStr += "<data>"; xmlStr += "<금액>2원</금액>"; xmlStr += "<안녕>hi</안녕>"; xmlStr += "</data>"; xmlStr += "</header>"; xmlStr += "<body>"; xmlStr += "<data>"; xmlStr += "<순번>1</순번>"; xmlStr += "<계좌>12-34</계좌>"; xmlStr += "<금액>10원</금액>"; xmlStr += "<거래점>양재</거래점>"; xmlStr += "</data>"; xmlStr += "<data>"; xmlStr += "<순번>2</순번>"; xmlStr += "<계좌>56-78</계좌>"; xmlStr += "<금액>20원</금액>"; xmlStr += "<거래점>도곡</거래점>"; xmlStr += "</data>"; xmlStr += "</body>"; xmlStr += "</root>"; Document doc = getStringToDOM(xmlStr); Element el = doc.getDocumentElement(); String rootNodeName = el.getNodeName(); System.out.println("rootNodeName: " + rootNodeName); String nodeName = ""; String nodeValue = ""; Node headerNode = el.getElementsByTagName("header").item(0); NodeList headerNodeChild = headerNode.getChildNodes(); for( int i=0; i<headerNodeChild.getLength(); i++ ){ NodeList dataNodeList = headerNodeChild.item(i).getChildNodes(); for( int j=0; j<dataNodeList.getLength(); j++ ){ nodeName = dataNodeList.item(j).getNodeName(); nodeValue = dataNodeList.item(j).getFirstChild().getNodeValue(); System.out.println("nodeName: " + nodeName + "=" + nodeValue); } System.out.println(""); }
java DOM 예제
2010. 9. 3. 11:02