KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.io.xml;
2
3 import nu.xom.*;
4
5 public class XomReader extends AbstractDocumentReader {
6
7     private Element currentElement;
8
9     public XomReader(Element rootElement) {
10         super(rootElement);
11     }
12
13     public XomReader(Document document) {
14         super(document.getRootElement());
15     }
16
17     public String JavaDoc getNodeName() {
18         return currentElement.getLocalName();
19     }
20
21     public String JavaDoc getValue() {
22         // currentElement.getValue() not used as this includes text of child elements, which we don't want.
23
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
24         int childCount = currentElement.getChildCount();
25         for(int i = 0; i < childCount; i++) {
26             Node child = currentElement.getChild(i);
27             if (child instanceof Text) {
28                 Text text = (Text) child;
29                 result.append(text.getValue());
30             }
31         }
32         return result.toString();
33     }
34
35     public String JavaDoc getAttribute(String JavaDoc name) {
36         return currentElement.getAttributeValue(name);
37     }
38
39     public String JavaDoc getAttribute(int index) {
40         return currentElement.getAttribute(index).getValue();
41     }
42
43     public int getAttributeCount() {
44         return currentElement.getAttributeCount();
45     }
46
47     public String JavaDoc getAttributeName(int index) {
48         return currentElement.getAttribute(index).getQualifiedName();
49     }
50
51     protected int getChildCount() {
52         return currentElement.getChildElements().size();
53     }
54
55     protected Object JavaDoc getParent() {
56         return currentElement.getParent();
57     }
58
59     protected Object JavaDoc getChild(int index) {
60         return currentElement.getChildElements().get(index);
61     }
62
63     protected void reassignCurrentElement(Object JavaDoc current) {
64         currentElement = (Element) current;
65     }
66 }
67
Popular Tags