Skip to content

Instantly share code, notes, and snippets.

@hackugyo
Last active December 28, 2015 07:19
Show Gist options
  • Save hackugyo/7463284 to your computer and use it in GitHub Desktop.
Save hackugyo/7463284 to your computer and use it in GitHub Desktop.
JSONICを使って,Android上でXMLをJSONに変換する
/**
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.arnx.jsonic.JSON;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.Menu;
*/
private void xmlToJson() {
HandlerThread thread = new HandlerThread("newThread");
thread.start();
Handler threadHandler = new Handler(hogeThread.getLooper());
threadHandler.post(new Runnable() {
@Override
public void run() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// factory.setValidating(true);
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse("http://www.webword.jp/xml/ini/sample1.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String xmljson = JSON.encode(doc);
LogUtils.i("JSON: " + xmljson);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment