0

我正在研究 Rss 提要。我正在使用 DOM 解析器

The actual link is:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNEd3ZGNLp5P-6bn44JmKbPfQimTyQ&url=http://www.foxnews.com/world/2012/09/21/libyans-storm-ansar-al-sharia-compound-in-backlash-attack-on-us-consulate/

After executing the function    xml = parser.getXmlFromUrl(URL);
URL=https://news.google.com/news/feeds?ned=us&topic=w&output=rss



In xml Iam getting the link as:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNHrclA7rxiQuEvAA_o9ZDRlZQFLkg&url=http://www.usatoday.com/news/world/story/2012/09/21/libyans-storm-militia-in-backlash-of-attack-on-us/57821580/1

当我将此网址粘贴到浏览器中时,它显示“这是一个无效的网址”。

How to get the a valid url? Could anyone please help me.

下面的函数是从 url 获取 xml:

public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
4

2 回答 2

0

尝试使用一段代码并根据您的变量进行更改....

try {
    // Create a URL for the desired page
        String stringURL = "http://link;
        URL url = new URL(stringURL);
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));
        while ((xml = in.readLine()) != null) {
            stringXml += xml;

        }
        in.close();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(new StringReader(stringXml)));
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("nodename");
            Node node = nodeLst.item(0);
            String demo1 = node.getTextContent();

            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo2= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo3= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo4= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo5= node.getTextContent();
            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            String demo6= node.getTextContent();


            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);

            nodeLst = doc.getElementsByTagName("nodename");
            node = nodeLst.item(0);
            password="+username.getText();
            }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-09-22T04:31:49.797 回答
0

我正在使用 dom 进行解析......很高兴了解这是在程序的单个活动中以最佳方式使用的非常非常快捷的方式。你以前没有见过我想的。

公共类 MainActivity 扩展 Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    /** Create a new textview array to display the results */
    TextView name[];
    TextView website[];
    TextView category[];

    try {

        URL url = new URL(
                "http://xyz.com/aa.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("item");

        /** Assign textview array lenght by arraylist size */
        name = new TextView[nodeList.getLength()];
        website = new TextView[nodeList.getLength()];
        category = new TextView[nodeList.getLength()];

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            name[i] = new TextView(this);
            website[i] = new TextView(this);
            category[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("name");
            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();
            name[i].setText("Name = "
                    + ((Node) nameList.item(0)).getNodeValue());

            NodeList websiteList = fstElmnt.getElementsByTagName("website");
            Element websiteElement = (Element) websiteList.item(0);
            websiteList = websiteElement.getChildNodes();
            website[i].setText("Website = "
                    + ((Node) websiteList.item(0)).getNodeValue());

            category[i].setText("Website Category = "
                    + websiteElement.getAttribute("category"));

            layout.addView(name[i]);
            layout.addView(website[i]);
            layout.addView(category[i]);

        }
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Set the layout view to display */
    setContentView(layout);

}

} ....................在manyfeast你可以添加

于 2013-10-22T00:10:34.523 回答