이전 질문 게시판은 새 글 쓰기를 막았습니다. [질문 게시판]을 이용바랍니다.
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은 '명문화된 삭제규정'이 반드시 필요하지 않은 분을 환영합니다.
법 없이도 사는 사람, 남에게 상처를 주지 않으면서 같이 이야기 나눌 수 있는 분이면 좋겠습니다."
|