DOMEventConsumer implements XMLEventConsumer { public static Document consume(XMLEventReader reader) throws XMLStreamException { DocumentBuilder documentBuilder; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new XMLStreamException(e); } return consume(reader, documentBuilder); } DOMEventConsumer(Node node);  DOMEventConsumer(Node node, boolean namespaceAware); @Override void add(XMLEvent event); void add(XMLEventReader reader); static Document consume(XMLEventReader reader); static Document consume(XMLEventReader reader, DocumentBuilder documentBuilder); static void consume(XMLEventReader reader, Node node);  }
@Test public void test1() throws XMLStreamException { StringReader json = new StringReader("{\"alice\":{\"@edgar\":\"david\",\"bob\":\"charlie\"}}"); XMLEventReader reader = new JsonXMLInputFactory().createXMLEventReader(json); Document node = DOMEventConsumer.consume(reader); Node alice = node.getChildNodes().item(0); Assert.assertEquals("alice", alice.getLocalName()); Assert.assertEquals("david", alice.getAttributes().getNamedItem("edgar").getNodeValue()); Node bob = alice.getChildNodes().item(0); Assert.assertEquals("bob", bob.getLocalName()); Assert.assertEquals("charlie", bob.getTextContent()); }
@Test public void test2() throws XMLStreamException { DocumentBuilder documentBuilder; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new XMLStreamException(e); } StringReader json = new StringReader("{\"alice\":{\"@edgar\":\"david\",\"bob\":\"charlie\"}}"); XMLEventReader reader = new JsonXMLInputFactory().createXMLEventReader(json); Document document = documentBuilder.newDocument(); Node node = document.createElement("foo"); document.appendChild(node); DOMEventConsumer.consume(reader, node); Node alice = node.getChildNodes().item(0); Assert.assertEquals("alice", alice.getLocalName()); Assert.assertEquals("david", alice.getAttributes().getNamedItem("edgar").getNodeValue()); Node bob = alice.getChildNodes().item(0); Assert.assertEquals("bob", bob.getLocalName()); Assert.assertEquals("charlie", bob.getTextContent()); }
@Test public void testXPath() throws XMLStreamException, XPathException { StringReader json = new StringReader("{\"alice\":{\"@edgar\":\"david\",\"bob\":\"charlie\"}}"); XMLEventReader reader = new JsonXMLInputFactory().createXMLEventReader(json); Document document = DOMEventConsumer.consume(reader); XPath xpath = XPathFactory.newInstance().newXPath(); Assert.assertEquals("charlie", xpath.evaluate(" }