1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 4 import org.w3c.dom.Document ; 5 import org.w3c.dom.Element ; 6 7 import javax.xml.parsers.DocumentBuilder ; 8 import javax.xml.parsers.DocumentBuilderFactory ; 9 import java.io.ByteArrayInputStream ; 10 11 public class DomReaderTest extends AbstractXMLReaderTest { 12 13 protected HierarchicalStreamReader createReader(String xml) throws Exception { 15 return new DomReader(buildDocument(xml)); 16 } 17 18 private Document buildDocument(String xml) throws Exception { 19 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 20 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 21 ByteArrayInputStream inputStream = new ByteArrayInputStream (xml.getBytes()); 22 Document document = documentBuilder.parse(inputStream); 23 return document; 24 } 25 26 public void testCanReadFromElementOfLargerDocument() throws Exception { 27 Document document = buildDocument("" + 28 "<big>" + 29 " <small>" + 30 " <tiny/>" + 31 " </small>" + 32 " <small-two>" + 33 " </small-two>" + 34 "</big>"); 35 Element small = (Element ) document.getDocumentElement().getElementsByTagName("small").item(0); 36 37 HierarchicalStreamReader xmlReader = new DomReader(small); 38 assertEquals("small", xmlReader.getNodeName()); 39 xmlReader.moveDown(); 40 assertEquals("tiny", xmlReader.getNodeName()); 41 } 42 43 } 44 | Popular Tags |