我正在尝试使用 DOM 解析器解析 XML 文件。XML 文件包含按名称、FAA 标识符、纬度/经度和 URL 列出的机场列表。这是它的一个片段:
<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>
我正在尝试创建包含每个已解析机场信息的对象(每个机场一个),以便我可以仅按名称显示每个机场。其余的机场信息将在稍后的项目中使用。
谁能告诉我如何根据这个 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);
}
}