Android dom解析器错误

时间:2011-09-01 12:17:58

标签: android parsing dom

我在解析xml时遇到了一个特殊的问题,所以解析器工作正常,直到它鼓励< Url />标签里面没有任何价值,我已经把测试作了我的代码:

static final String ImageHotel = "Url";

...

else if (name.equalsIgnoreCase("ImageHotel")){
                        message.setHotelImage(property.getFirstChild().getNodeValue());
                            if (!marchand.getImgHtlUrl().equalsIgnoreCase("")){
                                message.setHotelImageLink(new URL(marchand.getImgHtlUrl() + property.getFirstChild().getNodeValue()));
                            }else{
                                message.setHotelImageLink(new URL("http://localhost/noimage.jpg"));
                            }
                        }

即使我试图绕过try / catch来绕过它,这仍然会引发错误。

欢迎任何帮助

感谢。 Houssem。

2 个答案:

答案 0 :(得分:1)

我参考解析此文件尝试添加您自己的数据并获取标记值....

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

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 NewsParsing {

    NewsBeen Objnewsbeen;
    Vector<NewsBeen> vectParse;

    public NewsParsing() {
        System.out.println("Constructor is calling Now ...");

        try {

            vectParse = new Vector<NewsBeen>();
            // http://www.npr.org/rss/rss.php?id=1001
            URL url = new URL(
                    "http://www.npr.org/rss/rss.php?id=1001");
            URLConnection con = url.openConnection();

            System.out.println("Connection is : " + con);

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            System.out.println("Reader :" + reader);

            String inputLine;
            String fullStr = "";
            while ((inputLine = reader.readLine()) != null)
                fullStr = fullStr.concat(inputLine + "\n");

            InputStream istream = url.openStream();

            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();

            System.out.println("Builder : " + builder);

            Document doc = builder.parse(istream);

            System.out.println("Doc is : " + doc);

            doc.getDocumentElement().normalize();
            System.out.println("After Normlize : " + doc);

            System.out.println("Root is : "
                    + doc.getDocumentElement().getNodeName());

            System.out
                    .println("-------------------------------------------------------------------------------------------------------------");
            Element element = doc.getDocumentElement();

            parseFile(element);

            for (int index1 = 0; index1 < vectParse.size(); index1++) {
                NewsBeen ObjNB = (NewsBeen) vectParse.get(index1);

                System.out.println("Item No : " + index1);
                System.out.println();

                System.out.println("Title is : " + ObjNB.title);
                System.out.println("Description is : " + ObjNB.description);
                System.out.println("Pubdate is : " + ObjNB.pubdate);
                System.out.println("Link is : " + ObjNB.link);
                System.out.println("Guid is : " + ObjNB.guid);

                System.out.println();
                System.out
                        .println("-------------------------------------------------------------------------------------------------------------");

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void parseFile(Node node) {

        NodeList nodelist = node.getChildNodes();

        for (int index = 0; index < nodelist.getLength(); index++) {
            Node nodefromList = nodelist.item(index);

            if (nodefromList.getNodeType() == Node.ELEMENT_NODE) {
                // System.out.println("node.getNodeType() : " +
                // nodefromList.getNodeType());
                // System.out.println("Node is : " + node.getNodeName());

                if (nodefromList.getNodeName().equalsIgnoreCase("item")) {

                    Objnewsbeen = new NewsBeen();
                    vectParse.addElement(Objnewsbeen);
                }

                if (nodefromList.hasChildNodes()) {
                    if (nodefromList.getChildNodes().item(0).getNodeName()
                            .equals("#text")) {

                        if (!nodefromList.getChildNodes().item(0)
                                .getNodeValue().trim().equals("")
                                && Objnewsbeen != null)

                            if (nodefromList.getNodeName().equalsIgnoreCase(
                                    "title")) {
                                Objnewsbeen.title = nodefromList
                                        .getChildNodes().item(0).getNodeValue();

                            } else if (nodefromList.getNodeName()
                                    .equalsIgnoreCase("description")) {
                                Objnewsbeen.description = nodefromList
                                        .getChildNodes().item(0).getNodeValue();

                            } else if (nodefromList.getNodeName()
                                    .equalsIgnoreCase("pubDate")) {
                                Objnewsbeen.pubdate = nodefromList
                                        .getChildNodes().item(0).getNodeValue();

                            } else if (nodefromList.getNodeName()
                                    .equalsIgnoreCase("link")) {
                                Objnewsbeen.link = nodefromList.getChildNodes()
                                        .item(0).getNodeValue();

                            } else if (nodefromList.getNodeName()
                                    .equalsIgnoreCase("guid")) {
                                Objnewsbeen.guid = nodefromList.getChildNodes()
                                        .item(0).getNodeValue();

                            } else {
                                // System.out.println();
                            }
                    }
                    parseFile(nodefromList);
                }
            }
        }

    }

    public static void main(String[] args) {
        new NewsParsing();
    }

}

NewsBeen类是::

公共课NewsBeen {

public String title;
public String description;
public String pubdate;
public String link;
public String guid;

}

答案 1 :(得分:0)

我告诉过你,围绕着try / catch没有用,所以,现在确实如此,因为我必须跟踪NullPointerException而不是简单的Exception,如下所示:

   try{
      message.setHotelImage(property.getFirstChild().getNodeValue());
   }catch(NullPointerException nEx){
      marchand.setImgHtlUrl("");
   }

谢谢你们,抱歉这个提醒;)