Created
October 21, 2015 19:00
-
-
Save pgtwitter/e13e97b642f9f15ac245 to your computer and use it in GitHub Desktop.
XML String to Hashtable
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 java.io.StringReader; | |
import java.util.ArrayList; | |
import java.util.Hashtable; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathExpressionException; | |
import javax.xml.xpath.XPathFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.InputSource; | |
public class XML2Hashtable { | |
@SuppressWarnings("unchecked") | |
public static void main(String[] args) throws Exception { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); | |
sb.append("<companies>"); | |
sb.append("<company>"); | |
sb.append("<name>example.com</name>"); | |
sb.append("<url>http://www.example.com/</url>"); | |
sb.append("</company>"); | |
sb.append("<company>"); | |
sb.append("<name>example.jp</name>"); | |
sb.append("<url>http://www.example.jp/</url>"); | |
sb.append("</company>"); | |
sb.append("</companies>"); | |
String xmlString = sb.toString(); | |
InputSource is = new InputSource(); | |
is.setCharacterStream(new StringReader(xmlString)); | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document document = builder.parse(is); | |
{ | |
Hashtable<String, Object> obj = (Hashtable<String, Object>) node2hash(document); | |
Hashtable<String, Object> companies = (Hashtable<String, Object>) obj.get("companies"); | |
ArrayList<Hashtable<String, Object>> nlist = (ArrayList<Hashtable<String, Object>>) companies | |
.get("company"); | |
for (int i = 0; i < nlist.size(); i++) { | |
System.out.println("--- " + i + " ---"); | |
System.out.println(nlist.get(i).get("url")); | |
} | |
} | |
{ | |
NodeList nlist = evaluateXPath("//company", document); | |
for (int i = 0; i < nlist.getLength(); i++) { | |
System.out.println("--- " + i + " ---"); | |
System.out.println(node2hash(nlist.item(i)).get("url")); | |
} | |
} | |
{ | |
NodeList nlist = evaluateXPath("//company/url/text()", document); | |
for (int i = 0; i < nlist.getLength(); i++) { | |
System.out.println("--- " + i + " ---"); | |
System.out.println(nlist.item(i).getNodeValue()); | |
} | |
} | |
} | |
public static NodeList evaluateXPath(String xpathString, Document doc) throws IOException { | |
try { | |
XPathFactory factory = XPathFactory.newInstance(); | |
XPath xpath = factory.newXPath(); | |
return (NodeList) xpath.evaluate(xpathString, doc, XPathConstants.NODESET); | |
} catch (XPathExpressionException e) { | |
throw new IOException(e.getMessage()); | |
} | |
} | |
private static ArrayList<Node> childElements(Node node) { | |
NodeList nlist = node.getChildNodes(); | |
ArrayList<Node> ret = new ArrayList<Node>(); | |
for (int i = 0; i < nlist.getLength(); i++) { | |
Node n = nlist.item(i); | |
if (n.getNodeType() == Node.ELEMENT_NODE) | |
ret.add(n); | |
} | |
return ret; | |
} | |
@SuppressWarnings("unchecked") | |
private static Hashtable<String, Object> node2hash(Node node) { | |
Hashtable<String, Object> ret = new Hashtable<String, Object>(); | |
ArrayList<Node> cnodes = childElements(node); | |
for (Node n : cnodes) { | |
ArrayList<Node> cn = childElements(n); | |
String name = n.getNodeName(); | |
if (cn.size() > 0) { | |
if (ret.containsKey(name)) { | |
Object o = ret.get(name); | |
if (o instanceof Hashtable) { | |
ArrayList<Hashtable<String, Object>> ary = new ArrayList<Hashtable<String, Object>>(); | |
ret.put(name, ary); | |
ary.add((Hashtable<String, Object>) o); | |
ary.add(node2hash(n)); | |
} else if (o instanceof ArrayList) { | |
((ArrayList<Hashtable<String, Object>>) o).add(node2hash(n)); | |
} | |
} else { | |
ret.put(name, node2hash(n)); | |
} | |
} else { | |
ret.put(name, n.getFirstChild().getNodeValue()); | |
} | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment