我在Android development
转换XML to JSON
其中包括时遇到此错误CDATA tag
。它在正常情况下工作正常Java
。
我用过Library
。
org.json
.
public Document fetchXMLHTTP(String httpUrl) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // This removes CDATA Tag and converts it ", this doesn't work in Android
Log.v("xml_parser", "Coalescing Aware: " + factory.isCoalescing());
if(factory.isNamespaceAware()){
}
DocumentBuilder builder = factory.newDocumentBuilder();
URL uri = new URL(httpUrl);
Document doc = builder.parse(new InputSource(uri.openStream()));
doc.normalizeDocument();
NodeList xml = doc.getElementsByTagName("avatar");
Log.v("xml_parser", "Avatar Content: " + xml.item(0).getTextContent());
return doc;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
public String generateNewXML(Document doc, String tag) {
try {
NodeList xml = doc.getElementsByTagName(tag);
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(xml.item(0)), new StreamResult(sw));
String newXML = sw.toString();
System.out.println("New XML: " + newXML);
return newXML;
} catch (Exception e) {
e.getStackTrace();
}
return "";
}
我得到了这种类型的Error
.
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NoSuchMethodError: org.json.XMLTokener.end
at org.json.XMLTokener.nextCDATA(XMLTokener.java:69)
at org.json.XML.parse(XML.java:165)
at org.json.XML.parse(XML.java:272)
at org.json.XML.parse(XML.java:272)
at org.json.XML.toJSONObject(XML.java:369)
at com.fragdeluxestats.model.DataHandler.getPlayerSummary(DataHandler.java:85)
at com.fragdeluxestats.model.DataAsyncTask.doInBackground(DataAsyncTask.java:45)
at com.fragdeluxestats.model.DataAsyncTask.doInBackground(DataAsyncTask.java:19)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
XML 有两种类型tags
,如下所示:
<avatar><![CDATA[http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/11/11c82457c292cc278ab84c6e912d9668491f7d3e_full.jpg]]></avatar>