Abh*_*nde 3 android cdata xml-parsing
我正在解析服务器上的XML,我读它并解析它没有任何错误但是我无法看到数据.
这是我的XML:
<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book>
Run Code Online (Sandbox Code Playgroud)
我正在使用DocumentBuilderFactory即使我设置也看到了代码
dbf.setCoalescing(true);
Run Code Online (Sandbox Code Playgroud)
但仍然无法正常工作请参阅DocumentBuilderFactory的代码
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.d("XML parse Error:", e.getMessage());
return null;
} catch (SAXException e) {
Log.d("Wrong XML File Structure", e.getMessage());
return null;
} catch (IOException e) {
Log.d("IOException", e.getMessage());
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是示例XML:
<?xml version="1.1" encoding="utf-8"?>
<sample>
<artists>
<artist>
<artistname><![CDATA[+&&&Plus]]></artistname>
</artist>
<artist>
<artistname>015B</artistname>
</artist>
</artists>
</sample>
Run Code Online (Sandbox Code Playgroud)
假设您已经将xml文件内容作为xmlString,以下方法将使用或不使用cdata标记来解析xml:
private static final String XML_TAG = "artist";
private Object parseXML(String xmlString) throws Exception {
try {
Document doc = getDomElement(xmlString);
NodeList nodes = doc.getElementsByTagName(XML_TAG);
return retrieveData(doc,nodes);
} catch (Exception e) {
//Logger.logError(e);
throw e;
}
}
static public Document getDomElement(String xmlString) throws ParserConfigurationException, SAXException, IOException {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
doc = db.parse(is);
return doc;
}
private Object retrieveData(Document doc, NodeList nodes) {
ArrayList<String> list = new ArrayList<String>();
for(int i = 0 ; i < nodes.getLength() ; i++) {
Element element = (Element) nodes.item(i);
String name = getValue(element, "artistname");
list.add(name);
}
return list;
}
static public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return getElementValue(n.item(0));
}
static public final String getElementValue( Node elem ) {
try {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.CDATA_SECTION_NODE
|| child.getNodeType() == Node.TEXT_NODE )
{
return child.getNodeValue().trim();
}
}
}
}
return "";
} catch (DOMException e) {
//Logger.logError(e);
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11019 次 |
| 最近记录: |