Last active
December 28, 2015 07:19
-
-
Save hackugyo/7463284 to your computer and use it in GitHub Desktop.
JSONICを使って,Android上でXMLをJSONに変換する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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