解析java中的xml字符串?

parse an xml string in java?

如何解析存储在Java字符串对象中的XML?

Java的XMLRe读器只能解析URI或输入流中的XML文档。是否无法从包含XML数据的字符串进行分析?

现在我有以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
}

在我的处理程序上,我有:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
        }
}

提前谢谢


SAXParser可以读取输入源。

输入源可以在其构造函数中获取读取器

因此,可以通过StringReader放置解析XML字符串

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


使用一行代码尝试jcabi xml(请参阅本文):

1
XML xml = new XMLDocument("<document>...</document>")


您的XML可能足够简单,可以使用DOM或SAX API手动解析,但我仍然建议使用XML序列化API(如JAXB、XStream)或Simple,因为编写自己的XML序列化/反序列化代码很麻烦。

请注意,XStream FAQ错误地声称必须将生成的类与JAXB一起使用:

How does XStream compare to JAXB (Java API for XML Binding)?

JAXB is a Java binding tool. It generates Java code from a schema and
you are able to transform from those classes into XML matching the
processed schema and back. Note, that you cannot use your own objects,
you have to use what is generated.

看起来这是真的,但JAXB 2不再要求您使用从模式生成的Java类。

如果您走这条路线,请务必查看我提到的序列化/编组API的并行比较:

http://blog.bdougan.com/2010/10/how-do-jaxb-compare-to-xstream.htmlhttp://blog.bdougan.com/2010/10/how-do-jaxb-compare-to-simple.html


看看这个:http://www.rgagnon.com/javadetails/java-0573.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class ParseXMLString {

  public static void main(String arg[]) {
     String xmlRecords =
     "<data>" +
     "" +
     "   <name>John</name>" +
     "   Manager" +
     " </employee>" +
     "" +
     "   <name>Sara</name>" +
     "   Clerk" +
     " </employee>" +
     "</data>";

    try {
        DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlRecords));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("employee");

        // iterate the employees
        for (int i = 0; i < nodes.getLength(); i++) {
           Element element = (Element) nodes.item(i);

           NodeList name = element.getElementsByTagName("name");
           Element line = (Element) name.item(0);
           System.out.println("Name:" + getCharacterDataFromElement(line));

           NodeList title = element.getElementsByTagName("title");
           line = (Element) title.item(0);
           System.out.println("Title:" + getCharacterDataFromElement(line));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    /*
    output :
        Name: John
        Title: Manager
        Name: Sara
        Title: Clerk
    */
   

  }

  public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
       CharacterData cd = (CharacterData) child;
       return cd.getData();
    }
    return"?";
  }
}