Methods Summary |
---|
public java.lang.String | DOM2String(org.w3c.dom.Document aDocument, java.io.Writer aWriter)save document to stream.
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
} catch (javax.xml.transform.TransformerConfigurationException e) {
e.printStackTrace();
transformer = null;
throw e;
}
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
try {
transformer.transform(new DOMSource(aDocument),
new StreamResult(aWriter));
} catch (javax.xml.transform.TransformerException e) {
e.printStackTrace();
throw e;
}
return aWriter.toString();
|
private java.lang.String | fixNull(java.lang.String s)
return s == null ? "" : s;
|
public org.w3c.dom.NodeList | getChildElements(org.w3c.dom.Element aElement, java.lang.String aElementTagName)get the children of the same type element tag name.
NodeList nodeList = aElement.getChildNodes();
NodeListImpl list = new NodeListImpl();
int count = nodeList.getLength();
for (int i = 0; i < count; ++i) {
Node node = nodeList.item(i);
if (node instanceof Element) {
String tagName = getElementName((Element) node);
if (tagName.equals(aElementTagName)) {
list.add(node);
}
}
}
return list;
|
public org.w3c.dom.Element | getElement(org.w3c.dom.Document aParentDocument, java.lang.String aTagName)gets the element.
NodeList nodeList = aParentDocument.getElementsByTagName(aTagName);
return (nodeList != null) ? (Element) nodeList.item(0) : null;
|
public org.w3c.dom.Element | getElement(org.w3c.dom.Element elem, java.lang.String tagName)gets the element.
NodeList nl = elem.getElementsByTagName(tagName);
Element childElem = (Element) nl.item(0);
return childElem;
|
public java.lang.String | getElementName(org.w3c.dom.Element aElement)get Element Tag Name with striped prefix.
String tagName = aElement.getTagName();
return getName(tagName);
|
public java.lang.String | getElementValue(org.w3c.dom.Document doc, java.lang.String elemName)gets the element value.
String elemVal = null;
Element elem = getElement(doc, elemName);
elemVal = getTextData(elem);
return elemVal;
|
public org.w3c.dom.NodeList | getElements(org.w3c.dom.Element aParentElement, java.lang.String aTagName)gets list of elements.
return aParentElement.getElementsByTagNameNS(
aParentElement.getNamespaceURI(),
aTagName
);
|
public org.w3c.dom.NodeList | getElements(org.w3c.dom.Document aParentDocument, java.lang.String aTagName)gets set of elements.
return aParentDocument.getElementsByTagNameNS("*", aTagName);
|
public java.lang.String | getName(java.lang.String aName)strips the prefix of the name if present.
int lastIdx = aName.lastIndexOf(':");
if (lastIdx >= 0) {
return aName.substring(lastIdx + 1);
}
return aName;
|
private org.w3c.dom.Text | getText(org.w3c.dom.Element aElement)use this util method to create/retrieve a Text Node in a element.
Node node = null;
aElement.normalize();
node = aElement.getFirstChild();
if ((node == null) || !(node instanceof Text)) {
node = aElement.getOwnerDocument().createTextNode("");
aElement.appendChild(node);
}
return (Text) node;
|
public java.lang.String | getTextData(org.w3c.dom.Element aElement)use this util method to retrieve a Text Data in a element.
return getText(aElement).getData();
|
private void | resolvePrefix(org.w3c.dom.Element e, java.util.Map resolvedPrefixes)Inherit the namespace attributes from e's parents and set it
in e, if the prefix is not already resolved.
String nodePrefix = fixNull(e.getPrefix());
String nodeNamespace = e.getNamespaceURI();
if(nodeNamespace != null) {
/**
* Set the namespace attribute if it is not already set
*/
if(!resolvedPrefixes.containsKey(nodePrefix)) {
String attrName = (nodePrefix.trim().length() == 0)
? "xmlns" : "xmlns:" + nodePrefix;
e.setAttribute(attrName, nodeNamespace);
resolvedPrefixes.put(nodePrefix, nodeNamespace);
}
}
|
public void | setTextData(org.w3c.dom.Element aElement, java.lang.String aData)use this util method to set a Text Data in a element.
getText(aElement).setData(aData);
|
private void | writeAncestorNamespaceAttrs(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)
try {
String prefix = reader.getPrefix();
String namespace = ancestorNamespaceAttrs.remove(prefix);
if(namespace != null) {
writer.writeNamespace(prefix, namespace);
}
} catch(Exception ex) {
ex.printStackTrace();
}
|
void | writeAttributes(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)Writes namespace attributes and other attributes from the current node
of the reader to the current node of the writer.
int namespaceCount = reader.getNamespaceCount();
int attributeCount = reader.getAttributeCount();
for(int i=0; i<namespaceCount; i++) {
writer.writeNamespace(reader.getNamespacePrefix(i),
reader.getNamespaceURI(i));
}
for(int i=0; i<attributeCount; i++) {
String attrNs = reader.getAttributeNamespace(i);
if(attrNs != null) {
writer.writeAttribute(attrNs,
reader.getAttributeLocalName(i),
reader.getAttributeValue(i));
} else {
writer.writeAttribute(reader.getAttributeLocalName(i),
reader.getAttributeValue(i));
}
}
|
public void | writeChildren(javax.xml.stream.XMLStreamWriter writer, org.w3c.dom.Node node)
NodeList l = node.getChildNodes();
int size = l.getLength();
for ( int i = 0; i < size; i++ ) {
writeNode(l.item(i), writer, new HashMap<String,String>());
}
|
private void | writeChildren(javax.xml.stream.XMLStreamWriter writer, org.w3c.dom.Node node, java.util.Map resolvedPrefixes)This method should be used only from
writeNode(node,writer,resolvedPrefixes) method.
NodeList l = node.getChildNodes();
int size = l.getLength();
for ( int i = 0; i < size; i++ ) {
writeNode(l.item(i), writer, resolvedPrefixes);
}
|
public void | writeChildren(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)
writeChildren(reader, writer, new HashMap<String,String>());
|
public void | writeChildren(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)
reader.next();
while(reader.getEventType() != XMLStreamReader.END_ELEMENT) {
writeNode(reader, writer, ancestorNamespaceAttrs);
}
reader.next();
|
public void | writeNode(org.w3c.dom.Node node, javax.xml.stream.XMLStreamWriter writer)Writes the node to the XMLStreamWriter as text.
writeNode(node, writer, new HashMap<String,String>());
|
public void | writeNode(org.w3c.dom.Node node, javax.xml.stream.XMLStreamWriter writer, java.util.Map resolvedPrefixes)namespaceMap keeps track of the namespace attributes set
in the the ancestor node.
short type = node.getNodeType();
switch ( type ) {
case Node.DOCUMENT_NODE: {
writer.writeStartDocument();
writeChildren(writer, node, resolvedPrefixes);
break;
}
case Node.DOCUMENT_FRAGMENT_NODE: {
writeChildren(writer, node, resolvedPrefixes);
break;
}
case Node.ELEMENT_NODE: {
Element e = (Element)node;
String n = e.getTagName();
resolvePrefix(e, resolvedPrefixes);
writer.writeStartElement(n);
NamedNodeMap a = e.getAttributes();
int size = a.getLength();
for ( int i = 0; i < size; i++ ) {
Attr att = (Attr)a.item(i);
writeNode(att, writer, resolvedPrefixes);
}
if ( e.hasChildNodes() ) {
writeChildren(writer, node, resolvedPrefixes);
writer.writeEndElement();
} else
writer.writeEndElement();
break;
}
case Node.ATTRIBUTE_NODE:
Attr a = (Attr)node;
writer.writeAttribute(a.getName(), a.getValue());
break;
case Node.PROCESSING_INSTRUCTION_NODE: {
ProcessingInstruction pi = (ProcessingInstruction)node;
writer.writeProcessingInstruction(pi.getTarget(), pi.getData());
break;
}
case Node.TEXT_NODE: {
writer.writeCharacters(node.getNodeValue());
break;
}
case Node.CDATA_SECTION_NODE: {
writer.writeCData(node.getNodeValue());
break;
}
case Node.COMMENT_NODE: {
writer.writeComment(node.getNodeValue());
break;
}
}
|
public void | writeNode(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)writes the node pointed by XMLStreamReader to the XMLStreamWriter
Strictly speaking, this method should not be in this class.
writeNode(reader, writer, new HashMap<String,String>());
|
public void | writeNode(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)
int depth = 0;
int event = reader.getEventType();
do {
switch(event) {
case XMLStreamReader.START_DOCUMENT:
reader.next();
writeNode(reader, writer);
break;
case XMLStreamReader.START_ELEMENT:
String tagName =
(reader.getPrefix() != null && reader.getPrefix().length() != 0)
? reader.getPrefix() + ":" + reader.getLocalName()
: reader.getLocalName();
writer.writeStartElement(tagName);
writeAttributes(reader, writer);
writeAncestorNamespaceAttrs(reader, writer, ancestorNamespaceAttrs);
++depth;
break;
case XMLStreamReader.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamReader.END_ELEMENT:
writer.writeEndElement();
--depth;
}
event = reader.next();
} while(depth > 0);
|