반응형
xml문서를 파일에서 읽어오는 방법과 웹페이지로 읽어 오는 방법 모두 구현되어 있습니다
java 소스 입니다
import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; public class Db2ConnectGetContentsXml { public static void main(String[] args) { try { /* //파일 입출력 용 File file = new File("http:/파일 위치/my_test/mySampleServlet2?tn=neck_cold"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); doc.getDocumentElement().normalize(); */ String tableName = "neck_cold"; //url입출력용 URL url = new URL("http://주소/my_test/mySampleServlet2?tn="+tableName); SAXParserFactory parserModel = SAXParserFactory.newInstance(); SAXParser concreteParser = parserModel.newSAXParser(); XMLReader myReader = concreteParser.getXMLReader(); myReader.parse(new InputSource(url.openStream())); DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance(); DocumentBuilder builder2 = factory1.newDocumentBuilder(); Document doc3 = builder2.parse(new InputSource(url.openStream())); System.out.println("Root element " + doc3.getDocumentElement().getNodeName()); NodeList headline_node_list = doc3.getElementsByTagName("데이터"); Node headline_node = headline_node_list.item(0); if (headline_node.getNodeType() == Node.ELEMENT_NODE) { Element headline_element = (Element) headline_node; NodeList news_node_list = headline_element.getElementsByTagName("테이블"); for (int j = 0; j < news_node_list.getLength(); j++) { Node news_node = news_node_list.item(j); if (news_node.getNodeType() == Node.ELEMENT_NODE) { Element news_element = (Element) news_node; NodeList id_node_list0 = news_element.getElementsByTagName("글번호"); Node id_node0 = id_node_list0.item(0); System.out.println(id_node0.getNodeName() + " : " + id_node0.getTextContent()); NodeList id_node_list = news_element.getElementsByTagName("본문"); Node id_node = id_node_list.item(0); System.out.println(id_node.getNodeName() + " : " + id_node.getTextContent()); NodeList id_node_list1 = news_element.getElementsByTagName("아이디"); Node id_node1 = id_node_list1.item(0); System.out.println(id_node1.getNodeName() + " : " + id_node1.getTextContent()); NodeList id_node_list2 = news_element.getElementsByTagName("시간"); Node id_node2 = id_node_list2.item(0); System.out.println(id_node2.getNodeName() + " : " + id_node2.getTextContent()); NodeList id_node_list3 = news_element.getElementsByTagName("카테고리"); Node id_node3 = id_node_list3.item(0); System.out.println(id_node3.getNodeName() + " : " + id_node3.getTextContent()); NodeList id_node_list4 = news_element.getElementsByTagName("댓글갯수"); Node id_node4 = id_node_list4.item(0); System.out.println(id_node4.getNodeName() + " : " + id_node4.getTextContent()); NodeList id_node_list5 = news_element.getElementsByTagName("댓글위치"); Node id_node5 = id_node_list5.item(0); System.out.println(id_node5.getNodeName() + " : " + id_node5.getTextContent()); } } } } catch (Exception e) { System.out.println(e.getMessage()); //e.printStackTrace(); } } }
xml 소스 입니다
<테이블> <글번호>27 <본문>9컨텐츠내용 <아이디>paranmul@paranmul.pe.kr <시간>11.08.16 18:58 <카테고리>일상 <댓글갯수>0 <댓글위치>0 <테이블> <글번호>26 <본문>8컨텐츠내용 <아이디>paranmul@paranmul.pe.kr <시간>11.08.16 18:58 <카테고리>일상 <댓글갯수>0 <댓글위치>0
반응형