【问题标题】:Parse an online xml in java在java中解析在线xml
【发布时间】:2012-08-26 13:08:20
【问题描述】:

我正在尝试从

解析 xml

http://free.worldweatheronline.com/feed/weather.ashx?key=14d7241962022903123108&q=Pittsburgh,pa

我只需要标签“weatherDesc”中的一串天气描述

我试过了

    URL url = new URL("http://free.worldweatheronline.com/feed/weather.ashx?key=14d7241962022903123108&q=Pittsburgh,pa.xml");
    URLConnection conn = url.openConnection();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(conn.getInputStream());

我需要添加什么来挑选weatherDesc?

【问题讨论】:

    标签: java xml parsing


    【解决方案1】:

    为此使用SAX 解析器或DOM 解析器......

    我给你一个SAX 解析器的工作示例,用于parsing XML 形式的天气信息

    主要活动:

    public class WhetherXMLParsingAppActivity extends Activity {
    
        EditText editCity;
        EditText editState;
        TextView textCurrW;
        Button showButt;
        HandlingXmlStuff doingWork;
    
    
        final String baseURL ="http://www.google.com/ig/api?weather=";
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            editCity = (EditText)findViewById(R.id.edit_City);
            editState = (EditText)findViewById(R.id.edit_State);
            textCurrW = (TextView)findViewById(R.id.text_CurrWhether);
            showButt = (Button)findViewById(R.id.button_Show);
    
            showButt.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                     String c = editCity.getText().toString();
                     String s = editCity.getText().toString();
    
                     StringBuilder URL = new StringBuilder(baseURL);
                     URL.append( c + "," + s);
    
                     String fullURL = URL.toString();
    
    
                     try {
                        URL website = new URL(fullURL);
                         SAXParserFactory spf = SAXParserFactory.newInstance();
                         SAXParser sp = spf.newSAXParser();
                         XMLReader xr = sp.getXMLReader();
                         doingWork = new HandlingXmlStuff();
                         xr.setContentHandler(doingWork);
                         xr.parse(new InputSource(website.openStream()));
    
                         String endResult = doingWork.getInfo();
    
                         textCurrW.setText(endResult);
                    } catch (Exception e) {
                             textCurrW.setText("Error");
                    } 
    
    
    
                }
            });
    
        }
    }
    

    XML 处理类:

    public class HandlingXmlStuff extends DefaultHandler{
    
        XmlDataCollected xdc = new XmlDataCollected();
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            super.startElement(uri, localName, qName, attributes);
    
            if (localName.equals("city")) {
            String city = attributes.getValue("data");
            xdc.setCity(city);
    
            }else if (localName.equals("temp_f")) {
    
                String t = attributes.getValue("data");
                int temp = Integer.parseInt(t);
                xdc.setTemp(temp);
    
    
            }
    
    
        }
        public String getInfo() {
    
            return xdc.dataToString();
        }
    
    
    
    }
    

    数据处理类:

    public class XmlDataCollected {
    
        private int temp;
        private String city;
    
    
        public void setTemp(int temp) {
    
            this.temp = temp;
    
        }
    
        public void setCity(String city) {
    
            this.city = city;
        }
    
    
        public String dataToString() {
    
            return "In " + city + " the Temperature in Farenheit is " + temp + " degress ";
    
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      如果您只想要单个元素的值,我建议使用 JDK/JRE 中包含的javax.xml.xpath API。示例见:

      【讨论】:

        猜你喜欢
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        • 2012-06-29
        • 2017-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多