PGR21.com
이전 질문 게시판은 새 글 쓰기를 막았습니다. [질문 게시판]을 이용바랍니다.
Date 2010/03/31 23:32:49
Name tnpfpr
Subject 자바 질문있습니다.
<?xml version="1.0" encoding="euc-kr"?>
<forexml>
  <observation city="Austin, Texas" temp.F="75" rel_hum.percent="31" wind_speed.knt="8" skies="partly cloudy">
  
  </observation>
  <almanac sunrise="7:08 AM" sunset="6:21 PM" />
  <forecast type="nearterm" source="NWS" day="THIS AFTERNOON" weather="SU" high_temp="77" text="HIGHS 75 TO 80. WEST 15 TO 20 MPH." />
</forexml>


위의 xml 문서를 파싱하는 자바코드가 아래와 같이만들었습니다.

물론 실행도 되구요


import javax.xml.parsers.*;

import java.net.*;
import java.io.*;

import org.w3c.dom.*;

public class parseWeatherDOM {        
        
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new FileInputStream("_weather.xml"));
                
                Element root = doc.getDocumentElement();
                NodeList sub = root.getChildNodes();
                for(int i =0; i<sub.getLength();i++){
                        Node subNode = sub.item(i);
                        if(subNode.getNodeType() == Node.ELEMENT_NODE){
                                System.out.println(subNode.getNodeName());                                                // 노드명을 출력
//                                System.out.println(subNode.getNodeValue());                                                노드 값을 출력
                                NamedNodeMap  nnm = subNode.getAttributes();
                                for(int j=0; j<nnm.getLength();j++){
                                        Node att = nnm.item(j);
                                        if(subNode.getNodeType() == Node.ELEMENT_NODE){
                                                System.out.println(att.getNodeName() + ": ");                                                // 노드명을 출력
                                                System.out.println(att.getNodeValue());
                                        }
                                }
                                System.out.println();
                        }
                }
        }
}




<?xml version="1.0"?>
<forexml>
        <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
                <forecast_information>
                        <city>seoul</city>
                        <forecast_date>2010-03-25</forecast_date>
                        <unit_system>SI</unit_system>
                </forecast_information>
                <current_conditions>
                        <condition>대체로 흐림</condition>
                        <temp_f>45</temp_f>
                        <temp_c>7</temp_c>
                        <humidity data="습도">53%</humidity>
                        <wind_condition data="바람">북풍,8km/h</wind_condition>
                </current_conditions>
                <forecast_conditions week="목">
                        <low>-2</low>
                        <high>10</high>
                        <condition>한때 비</condition>
                </forecast_conditions>
                <forecast_conditions week="금">
                        <low>0</low>
                        <high>7</high>
                        <condition>맑음</condition>
                </forecast_conditions>
        </weather>
</forexml>

이제 위의 xml코드를 파싱하는 자바코드를 만들고있는데
원래 만든 소스에서 포문만 몇번 추가하면 될 것 같은데.

잘안되네요..

import javax.xml.parsers.*;

import java.net.*;
import java.io.*;

import org.w3c.dom.*;

public class parseWeatherDOM {        
        
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new FileInputStream("_weather.xml"));
                
                Element root = doc.getDocumentElement();
                NodeList sub = root.getChildNodes();
                for(int i=0; i<sub.getLength();i++){;
                Node subNode=sub.item(i);
                       NodeList sub2 = subNode.getChildNodes();
                for(int j=0; j<sub2.getLength();j++){
                Node subNode2 = sub2.item(j);
                NodeList sub3 = subNode2.getChildNodes();
                if(subNode2.getNodeType() == Node.ELEMENT_NODE)
                System.out.println(subNode2.getNOdeName());
                for(int m=0; m<sub3.getLength();m++){
                Node subNode3 = sub3.item(m);
                if(subNode3.getNodeType() == Node.ELEMENT_NODE){
                System.out.println(subNOde3.getNodeName() + " : ");
                System.out.println(subNOde3.getNodeValue());
        }


통합규정 1.3 이용안내 인용

"Pgr은 '명문화된 삭제규정'이 반드시 필요하지 않은 분을 환영합니다.
법 없이도 사는 사람, 남에게 상처를 주지 않으면서 같이 이야기 나눌 수 있는 분이면 좋겠습니다."
arq.Gstar
10/03/31 23:36
수정 아이콘
아~~까 낮에 질문 올라왔떤것 같은데.. -_-;;
아까 연구실에서 소스 코드 복사해서 만지작 하다가 게시판 보니까 지워졌길래 안봤는데..
혹시 내일까지 댓글 안올라오면 내일 다시 봐야겠네요 ^^
10/03/31 23:50
수정 아이콘
감사합니다 ^^; 낮에 하다가 계속 만져보고 있는데 잘 안되네요 ;;
목록 삭게로! 맨위로
번호 제목 이름 날짜 조회
78232 수면시간 [8] 一切唯心造2158 10/04/01 2158
78231 영덕에 내려가게 되었습니다. 대게 질문 [7] 라임1668 10/04/01 1668
78230 노트북 질문 드립니다! [1] 킬번1637 10/04/01 1637
78229 파이썬 조작 맵or 핵 조심하세요.. [11] 보라도리2615 10/04/01 2615
78228 소화되는데 시간이 많이 걸리는 느낌입니다 [1] 아레스1579 10/04/01 1579
78227 모임이름좀 지어주세요... [21] 하루를산다9772 10/04/01 9772
78226 컴퓨터 부품 구입 질문입니다. [5] EZrock1805 10/04/01 1805
78225 STX라는 회사를 다니시거나 잘 알고계신분 계신가요? [2] 불대가리2465 10/04/01 2465
78224 크롬에서 pgr 정상작동하고 있나요? [2] 소인배2148 10/04/01 2148
78223 웹디자인 프로그램 질문 있습니다~ [1] DS1948 10/04/01 1948
78222 서버형 컴퓨터에 대해서 질문이 있습니다. [3] Roro1911 10/04/01 1911
78220 이거 사실인가요?? 멀면 벙커링2093 10/04/01 2093
78219 후로리그에 대해서.. [3] 백소1814 10/04/01 1814
78218 외국에서 만우절이란게 어떤 의미가 있나요? [1] 부엉이2008 10/04/01 2008
78217 이준구, 이창용교수님의 경제학원론 연습문제와 해답 3판/4판이 차이가 없나요?? [2] 하성현3693 10/04/01 3693
78216 주식에 대한 간단한 질문입니다.. [8] 완소히드라1766 10/04/01 1766
78215 한국지리 질문입니다! [8] 따듯한담요2161 10/04/01 2161
78214 인터넷 응답없음 어떻게 해결하나요? [1] 뿌지직2655 10/04/01 2655
78213 후발 기업이 선발 기업을 앞서는 경우... [13] 동네노는아이2539 10/04/01 2539
78210 에시앙은 어떤 선수입니까? [16] 창천룡3531 10/03/31 3531
78209 바탕화면의 휴지통을 지워버렸습니다. [7] kikira2207 10/03/31 2207
78208 자바 질문있습니다. [2] tnpfpr2147 10/03/31 2147
78207 어머니께서 병원에 안가려고 하셔서.. (의료쪽 질문입니다.) [4] Newbie1651 10/03/31 1651
목록 이전 다음
댓글

+ : 최근 6시간내에 달린 댓글
+ : 최근 12시간내에 달린 댓글
맨 위로