如何使用SAX解析器在java中读取具有多个子节点的xml文件

时间:2013-06-26 09:35:02

标签: java xml xml-parsing sax saxparser

我怎样才能读取一个包含节点然后又是子节点的xml文件,这个子节点还可以有子节点

喜欢XML文件

<?xml version="1.0" encoding="UTF-8"?>
<school>
    <student id="1">
        <firstname name="ankush">
            <test>sample </test>  //......here can be more sub nodes

        </firstname>
        <lastname>thakur</lastname>
        <email>beingjavaguy.gmail.com</email>
        <phone>7678767656</phone>
    </student>

</school>

我曾经读过的代码:

public class ReadXml extends DefaultHandler{

public void getXml(){
        try {

            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            SAXParser saxParser = saxParserFactory.newSAXParser();

            DefaultHandler defaultHandler = new DefaultHandler(){

                String firstNameTag="close";
                String lastNameTag="close";
                String emailTag="close";
                String phoneTag="close";
                String testTag="close";

                public void startElement(String uri, String localName, String qName,
                        Attributes attributes) throws SAXException {

                    if (qName.equalsIgnoreCase("FIRSTNAME")) {
                        firstNameTag = "open";
                    }
                    if (qName.equalsIgnoreCase("TEST")) {
                        testTag = "open";
                    }

                    if (qName.equalsIgnoreCase("LASTNAME")) {
                        lastNameTag = "open";
                    }
                    if (qName.equalsIgnoreCase("EMAIL")) {
                        emailTag = "open";
                    }
                    if (qName.equalsIgnoreCase("PHONE")) {
                        phoneTag = "open";
                    }
                }


                public void characters(char ch[], int start, int length)
                        throws SAXException {

                    if (firstNameTag.equals("open")) {
                        System.out.println("First Name : " + new String(ch, start, length));
                    }
                    if (testTag.equals("open")) {
                        System.out.println("Test Name : " + new String(ch, start, length));
                    }

                    if (lastNameTag.equals("open")) {
                        System.out.println("Last Name : " + new String(ch, start, length));
                    }
                    if (emailTag.equals("open")) {
                        System.out.println("Email : " + new String(ch, start, length));
                    }
                    if (phoneTag.equals("open")) {
                        System.out.println("Phone : " + new String(ch, start, length));
                    }
                }


                public void endElement(String uri, String localName, String qName)
                        throws SAXException {

                    if (qName.equalsIgnoreCase("firstName")) {
                        firstNameTag = "close";
                    }
                    if (qName.equalsIgnoreCase("test")) {
                        testTag = "close";
                    }

                    if (qName.equalsIgnoreCase("lastName")) {
                        lastNameTag = "close";
                    }
                    if (qName.equalsIgnoreCase("email")) {
                        emailTag = "close";
                    }
                    if (qName.equalsIgnoreCase("phone")) {
                        phoneTag = "close";
                    }
                }
            };

            saxParser.parse("xmlToRead/student.xml", defaultHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OUTPUT

First Name : 

First Name : sample 
Test Name : sample 
First Name : 


Last Name : thakur
Email : beingjavaguy.gmail.com
Phone : 7678767656

任何帮助都会被贬低。

2 个答案:

答案 0 :(得分:0)

作为一种中间方式,可以使用DOM4J处理子树,然后将它们分离,从而最大限度地减少内存消耗:

    SAXReader reader = new SAXReader();

    reader.addHandler("/school/student", new ElementHandler() {

        @Override
        public void onStart(ElementPath elementPath) {
            // nothing
        }

        @Override
        public void onEnd(ElementPath elementPath) {
            Element student = elementPath.getCurrent();
            /*
             * process the current student, then detach it from the tree
             */
            student.detach();
        }
    });

    reader.read(...));

此处,该文档仅包含一个&lt; student&gt;元素一次。

答案 1 :(得分:0)

您要求我们教您如何编写SAX应用程序。有关于这个主题的书籍和教程:阅读它们。请注意,这并不容易。 Elliotte Rusty Harold关于XML和Java的书很棒。