Android dom 解析器错误

发布于 2024-12-02 05:52:32 字数 799 浏览 0 评论 0原文

我在解析 xml 时遇到一个特殊问题,因此解析器工作正常,直到遇到>标签不包含任何值,我已经在我的代码中进行了测试:

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 绕过它,这也会不断抛出错误。

欢迎任何帮助,

谢谢。 侯赛姆.

I'm facing a particular problem while parsing xml so the parser works fine until it encouters <Url/> tag which contains no value, I already made the test il my code :

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"));
                            }
                        }

This keep throwing error even when i tried to bypass by surrounding with try/catch..

any help is welcome

thanks.
Houssem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2024-12-09 05:52:32

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

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 类是::

public class NewsBeen {

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

}

I have reference to parse this file try to add your own data and get tag value....

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();
    }

}

The NewsBeen class is::

public class NewsBeen {

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

}

半夏半凉 2024-12-09 05:52:32

我告诉过你用 try/catch 包围不起作用,所以,现在它起作用了,因为我必须跟踪 NullPointerException 而不是简单的异常,就像这样:

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

谢谢你们,对这个警报感到抱歉;)

I told you that surrounding with try/catch did not work, so, now it does, because i had to track the NullPointerException instead of simple Exception, like this :

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

thank you guys and sorry for this alert ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文