KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > DomReaderTest


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
4 import org.w3c.dom.Document JavaDoc;
5 import org.w3c.dom.Element JavaDoc;
6
7 import javax.xml.parsers.DocumentBuilder JavaDoc;
8 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
9 import java.io.ByteArrayInputStream JavaDoc;
10
11 public class DomReaderTest extends AbstractXMLReaderTest {
12
13     // factory method
14
protected HierarchicalStreamReader createReader(String JavaDoc xml) throws Exception JavaDoc {
15         return new DomReader(buildDocument(xml));
16     }
17
18     private Document JavaDoc buildDocument(String JavaDoc xml) throws Exception JavaDoc {
19         DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
20         DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
21         ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(xml.getBytes());
22         Document JavaDoc document = documentBuilder.parse(inputStream);
23         return document;
24     }
25
26     public void testCanReadFromElementOfLargerDocument() throws Exception JavaDoc {
27         Document JavaDoc document = buildDocument("" +
28                 "<big>" +
29                 " <small>" +
30                 " <tiny/>" +
31                 " </small>" +
32                 " <small-two>" +
33                 " </small-two>" +
34                 "</big>");
35         Element JavaDoc small = (Element JavaDoc) 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