我想将字符串转换为org.jdom.Element
String s = "<rdf:Description rdf:about=\"http://dbpedia.org/resource/Barack_Obama\">";
我该怎么做?
答案 0 :(得分:4)
从字符串中解析XML的方法不止一种:
String xml = "Your XML";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
使用可以读取输入源的SAXParser:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
saxParser.parse(new InputSource(new StringReader("Your XML")), handler);
请参阅:SAXParser,InputSource
答案 1 :(得分:0)
Document
。查看JDOM FAQ:How do I construct a Document from a String? Document.getRootElement()
访问根元素。(你提到了包org.jdom
所以我假设你使用JDOM 1.1。)
答案 2 :(得分:-1)
我建议你使用http://simple.sourceforge.net/.With这个框架中的Simple XML Framework,你可以轻松地序列化和反序列化你的对象。我希望这些信息对您来说很重要。