Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

parse an xml string in java?

how do you parse xml stored in a java string object?

Java's XMLReader only parses XML documents from a URI or inputstream. is it not possible to parse from a String containing an xml data?

Right now I have the following:

try {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser sp = factory.newSAXParser();
    XMLReader xr = sp.getXMLReader(); 

    ContactListXmlHandler handler = new ContactListXmlHandler();
    xr.setContentHandler(handler);
    xr.p
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And on my handler i have this:

public class ContactListXmlHandler extends DefaultHandler implements Resources {

    private List<ContactName> contactNameList = new ArrayList<ContactName>();

    private ContactName contactItem;

    private StringBuffer sb;

    public List<ContactName> getContactNameList() {
        return contactNameList;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.startDocument();

        sb = new StringBuffer();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qName, attributes);
        if(localName.equals(XML_CONTACT_NAME)){
            contactItem = new ContactName();
        }

        sb.setLength(0);

    }

    @Override
    public void characters(char[] ch, int start, int length){
        // TODO Auto-generated method stub
        try {
            super.characters(ch, start, length);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sb.append(ch, start, length);
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }

    /**
     * where the real stuff happens
     */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        //super.endElement(arg0, arg1, arg2);

        if(contactItem != null){
            if (localName.equalsIgnoreCase("title")) {
                contactItem.setUid(sb.toString());
                Log.d("handler", "setTitle = " + sb.toString());

            } else if (localName.equalsIgnoreCase("link")) {
                contactItem.setFullName(sb.toString());

            } else if (localName.equalsIgnoreCase("item")){
                Log.d("handler", "adding rss item");
                contactNameList.add(contactItem);
            }

            sb.setLength(0);
        }
}

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The SAXParser can read an InputSource.

An InputSource can take a Reader in its constructor

So, you can put parse XML string via a StringReader

new InputSource(new StringReader("... your xml here....")));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...