从android中的网络数据库解析xml文件

发布于 2024-10-30 04:56:30 字数 3771 浏览 1 评论 0原文

我正在尝试从 URL 解析 xml 文件。我在以下链接中找到了一个示例

http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html
并尝试在我的代码中使用它,但它返回的值为 null

以下是我解析 xml 的代码

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

    URL url = new URL("http://www.siva.com/search");

    /** Handling XML */
    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
    SAXParser saxparser = saxparserfactory.newSAXParser();
    XMLReader xmlreader = saxparser.getXMLReader();

    /* Create a new ContentHandler and apply it to the XML-Reader*/
    ForListXMLHandler forlistmyhandler = new ForListXMLHandler();
    xmlreader.setContentHandler(forlistmyhandler);

    /* Parse the xml-data from our URL. */
    xmlreader.parse(new InputSource(url.openStream()));
    /* Parsing has finished. */

    /* Our ExampleHandler now provides the parsed data to us. */
    ParsedDataSet parsedDataSet = forlistmyhandler.getParsedData();
    System.out.println(parsedDataSet.toString());
}

以下是 MyXMLhandler 的代码

public class ForListXMLHandler extends DefaultHandler {
    private boolean in_outertag = false;
    private boolean in_innertag = false;
    private boolean in_First_name = false;
    private boolean in_Last_name = false;

    private ParsedDataSet myParsedDataSet = new ParsedDataSet();

    public ParsedDataSet getParsedData() {
        return this.myParsedDataSet;
    }

    @Override
    public void startDocument() throws SAXException {
        this.myParsedDataSet = new ParsedDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
        // Nothing to do
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equals("Searchdata")) {
            this.in_outertag = true;
        } else if (localName.equals("Searchdata")) {
            this.in_innertag = true;
        } else if (localName.equals("First_name")) {
            this.in_First_name = true;
        } else if (localName.equals("Last_name")) {
            this.in_Last_name = true;
        }
    }

    /**
     * Gets be called on closing tags like:
     * */
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (localName.equals("Searchdata")) {
            this.in_outertag = false;
        } else if (localName.equals("Searchdata")) {
            this.in_innertag = false;
        } else if (localName.equals("First_name")) {
            this.in_First_name = false;
        } else if (localName.equals("Last_name")) {
            // Nothing to do here
        }
    }

    /**
     * Gets be called on the following structure: characters
     */
    @Override
    public void characters(char ch[], int start, int length) {
        if (this.in_First_name) {
            myParsedDataSet.setfirstname(new String(ch, start, length));
        }
        if (this.in_Last_name) {
            myParsedDataSet.setlastname(new String(ch, start, length));
        }
    }
}

下一部分是我解析的数据集类

public class ParsedDataSet {
    private String First_name = null;
    private String Last_name = null;

    public String getFirstname() {
        return First_name;
    }

    public void setfirstname(String First_name) {
        this.First_name = First_name;
    }

    public String getlastname() {
        return Last_name;
    }

    public void setlastname(String Last_name) {
        this.Last_name = Last_name;
    }

    public String toString() {
        return this.First_name + "n" + this.Last_name;
    }
}

请告诉我哪里出错了

i am trying to parse an xml file from an URL. I found an example in the following link

http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html
and tried using it in my code but it returned the values to be as null

Following is my code of parsing xml

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

    URL url = new URL("http://www.siva.com/search");

    /** Handling XML */
    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
    SAXParser saxparser = saxparserfactory.newSAXParser();
    XMLReader xmlreader = saxparser.getXMLReader();

    /* Create a new ContentHandler and apply it to the XML-Reader*/
    ForListXMLHandler forlistmyhandler = new ForListXMLHandler();
    xmlreader.setContentHandler(forlistmyhandler);

    /* Parse the xml-data from our URL. */
    xmlreader.parse(new InputSource(url.openStream()));
    /* Parsing has finished. */

    /* Our ExampleHandler now provides the parsed data to us. */
    ParsedDataSet parsedDataSet = forlistmyhandler.getParsedData();
    System.out.println(parsedDataSet.toString());
}

following is the code of MyXMLhandler

public class ForListXMLHandler extends DefaultHandler {
    private boolean in_outertag = false;
    private boolean in_innertag = false;
    private boolean in_First_name = false;
    private boolean in_Last_name = false;

    private ParsedDataSet myParsedDataSet = new ParsedDataSet();

    public ParsedDataSet getParsedData() {
        return this.myParsedDataSet;
    }

    @Override
    public void startDocument() throws SAXException {
        this.myParsedDataSet = new ParsedDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
        // Nothing to do
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equals("Searchdata")) {
            this.in_outertag = true;
        } else if (localName.equals("Searchdata")) {
            this.in_innertag = true;
        } else if (localName.equals("First_name")) {
            this.in_First_name = true;
        } else if (localName.equals("Last_name")) {
            this.in_Last_name = true;
        }
    }

    /**
     * Gets be called on closing tags like:
     * */
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (localName.equals("Searchdata")) {
            this.in_outertag = false;
        } else if (localName.equals("Searchdata")) {
            this.in_innertag = false;
        } else if (localName.equals("First_name")) {
            this.in_First_name = false;
        } else if (localName.equals("Last_name")) {
            // Nothing to do here
        }
    }

    /**
     * Gets be called on the following structure: characters
     */
    @Override
    public void characters(char ch[], int start, int length) {
        if (this.in_First_name) {
            myParsedDataSet.setfirstname(new String(ch, start, length));
        }
        if (this.in_Last_name) {
            myParsedDataSet.setlastname(new String(ch, start, length));
        }
    }
}

next part is of my parsed data set class

public class ParsedDataSet {
    private String First_name = null;
    private String Last_name = null;

    public String getFirstname() {
        return First_name;
    }

    public void setfirstname(String First_name) {
        this.First_name = First_name;
    }

    public String getlastname() {
        return Last_name;
    }

    public void setlastname(String Last_name) {
        this.Last_name = Last_name;
    }

    public String toString() {
        return this.First_name + "n" + this.Last_name;
    }
}

pls tell me where i am getting error

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-11-06 04:56:30

endElement 方法在 characters 方法之前触发,因此当 characters 方法执行时,您的布尔变量始终设置为 false方法被解雇。您应该将一些代码从 endElement 移动到 characters,如下所示:

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

}

@Override
public void characters(char ch[], int start, int length) {
    if (this.in_First_name) {
        this.in_First_name = false;
        myParsedDataSet.setfirstname(new String(ch, start, length));
    }
    if (this.in_Last_name) {
       this.in_Last_name = false;
       myParsedDataSet.setlastname(new String(ch, start, length));
    }
}

您还应该看看 此处 有关“在 Android 上使用 XML”的完整说明。

The endElement method gets fired before the characters method, so your boolean variables are always set to false when the characters method gets fired. You should move some code from endElement to characters, something like this:

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

}

@Override
public void characters(char ch[], int start, int length) {
    if (this.in_First_name) {
        this.in_First_name = false;
        myParsedDataSet.setfirstname(new String(ch, start, length));
    }
    if (this.in_Last_name) {
       this.in_Last_name = false;
       myParsedDataSet.setlastname(new String(ch, start, length));
    }
}

You should also take a look here for a complete explanation on "Working with XML on Android".

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