DOM 파서를 사용하여 XML 파일을 구문 분석하려고합니다. XML 파일에는 이름, FAA 식별자, 위도/경도 및 URL별로 공항 목록이 포함되어 있습니다. 난 단지 이름으로 각 공항을 표시 할 수 있도록자바에서 DOM 파서로 객체를 만드시겠습니까?
<station>
<station_id>NFNA</station_id>
<state>FJ</state>
<station_name>Nausori</station_name>
<latitude>-18.05</latitude>
<longitude>178.567</longitude>
<html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url>
</station>
<station>
<station_id>KCEW</station_id>
<state>FL</state>
<station_name>Crestview, Sikes Airport</station_name>
<latitude>30.79</latitude>
<longitude>-86.52</longitude>
<html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url>
</station>
I 개체를 만들려고하고있다 (각 공항에 대한 하나) 각각의 정보를 포함, 공항 구문 분석 : 여기의 조각이다. 나머지 공항 정보는 나중에 프로젝트에서 사용됩니다.
누구나이 DOM 파서가 제공 한 정보에서 어떻게 개체를 만들고 인스턴스화하여 각 공항의 이름 만 표시 할 수 있는지 알려주실 수 있습니까?
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
//initialize variables
String station_id;
String state;
String station_name;
double latitude;
double longitude;
String html_url;
//Here is my constructor, I wish to instantiate these values with
//those of obtained by the DOM parser
Test(){
station_id = this.station_id;
state = this.state;
station_name = this.station_name;
latitude = this.latitude;
longitude = this.longitude;
html_url = this.html_url;
}
//Method for DOM Parser
public void readXML(){
try {
//new xml file and Read
File file1 = new File("src/flwxindex3.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file1);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("station");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element first = (Element) node;
station_id = first.getElementsByTagName("station_id").item(0).getTextContent();
state = first.getElementsByTagName("state").item(0).getTextContent();
station_name = first.getElementsByTagName("station_name").item(0).getTextContent();
latitude = Double.parseDouble(first.getElementsByTagName("latitude").item(0).getTextContent());
longitude = Double.parseDouble(first.getElementsByTagName("longitude").item(0).getTextContent());
html_url = first.getElementsByTagName("station_id").item(0).getTextContent();
}
/*These are just test to see if the actually worked
*
System.out.println(station_id);
System.out.println(state);
System.out.println(station_name);
System.out.println(latitude);
System.out.println(longitude);
System.out.println(html_url);
*/
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
public static void main(String[] args) {
//Create new object and call the DOM Parser method
Test test1 = new Test();
test1.readXML();
//System.out.println(test1.station_name);
}
}
감사하지만 나는 더 많은 질문이 있습니다. DOM 내부에서 변수 (stationName 등)를 해결하는 데 문제가 있습니다. 나는 내가 새로운 공항 종류와 목록을 오해했다고 생각한다. 이 오류를 수정할 수 있도록 어디서 수정해야합니까? 나는 getters/setters도 만들었습니다. 어디서나 생성자가 필요합니까? 기본적으로 DOM은 더 이상 내 변수에 액세스 할 수 없습니다. – David
DOM 내부에서 변수를 해결할 수 없습니다. 위 예제에서와 같이 getters/setters를 사용해야합니다. 'Object'는 명시 적으로 제공되지 않는 한 항상 [noargs 생성자] (http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html)를 가지고 있습니다. 아무 것도 필요하지 않으므로 생성자를 정의 할 필요가 없습니다. 이것은 [Java Bean] (http://en.wikipedia.org/wiki/JavaBeans)의 예입니다. –
자바 메서드를 사용하여 각 필드의 별칭 이름을 설정할 수 없습니까? 또는 주석? –