1

I am trying to parse the file below. I want to print the id and name of each passenger. Can you give me code to parse it ?

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="www.google.com">

<passenger id = "001">
<name>Tom Cruise</name>
</passenger>

<passenger id = "002">
<name>Tom Hanks</name>
</passenger>

</root>

UPDATE This is what i had tried. Code, problems etc mentioned here - Error in output of a simple SAX parser

4

4 回答 4

2

这是一个开始的工作示例,虽然我建议您改用 StAX,但您会发现 SAX 不是很方便

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAX2 {

    public static void main(String[] args) throws Exception {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(new File("test.xml"), new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes atts) throws SAXException {
                if (qName.equals("passenger")) {
                    System.out.println("id = " + atts.getValue(0));
                }
            }

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

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                String text = new String(ch, start, length);
                if (!text.trim().isEmpty()) {
                    System.out.println("name " + text);
                }
            }
        });
    }
}

输出

id = 001
name Tom Cruise
id = 002
name Tom Hanks
于 2012-12-11T11:29:24.867 回答
1
  1. 创建一个DocumentBuilderFactory.
  2. DocumentBuilder从工厂获得一个。
  3. 使用parse()构建器的一种方法来创建一个Document.
  4. 一旦你有了 a ,你就可以用's方法Document得到乘客了。ElementDocumentgetElementsByTagName()

我相信你将能够解决剩下的问题。

于 2012-12-11T10:34:45.287 回答
0
SAXParserFactory factory = SAXParserFactory.newInstance();
try {

    InputStream    xmlInput  = new FileInputStream("theFile.xml");
    SAXParser      saxParser = factory.newSAXParser();

    DefaultHandler handler   = new SaxHandler();
    saxParser.parse(xmlInput, handler);

} catch (Throwable err) {
    err.printStackTrace ();
}
于 2012-12-11T11:14:38.047 回答
0
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " + 
                         "<root xmlns:android=\"www.google.com\">" +
                            "<passenger id = \"001\">" +
                                "<name>Tom Cruise</name>" +
                            "</passenger>" +
                            "<passenger id = \"002\">" +
                                "<name>Tom Hanks</name>" +
                            "</passenger>" +
                         "</root>";
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
            DocumentBuilder db = dbf.newDocumentBuilder(); 
            InputSource is = new InputSource(new StringReader(str)); 

            final Document document = db.parse(is); 

            System.out.println("node Name " + document.getChildNodes().item(0).getChildNodes().item(1).getNodeName());
于 2012-12-11T11:36:39.583 回答