Fixed #75
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.r11.tools.xml;
|
package com.r11.tools.xml;
|
||||||
|
|
||||||
import org.apache.xpath.XPathAPI;
|
import org.apache.xpath.XPathAPI;
|
||||||
|
import org.apache.xpath.objects.XObject;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.traversal.NodeIterator;
|
import org.w3c.dom.traversal.NodeIterator;
|
||||||
@@ -76,28 +77,33 @@ public class Xalan {
|
|||||||
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
|
|
||||||
// Use the simple XPath API to select a nodeIterator.
|
// Use the simple XPath API to select a nodeIterator.
|
||||||
NodeIterator nl = XPathAPI.selectNodeIterator(doc, transform);
|
try {
|
||||||
|
NodeIterator nl = XPathAPI.selectNodeIterator(doc, transform);
|
||||||
|
|
||||||
// Serialize the found nodes to result object.
|
// Serialize the found nodes to result object.
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
Node n;
|
Node n;
|
||||||
while ((n = nl.nextNode())!= null) {
|
while ((n = nl.nextNode())!= null) {
|
||||||
StringBuilder sb;
|
StringBuilder sb;
|
||||||
if (isTextNode(n)) {
|
if (isTextNode(n)) {
|
||||||
// DOM may have more than one node corresponding to a
|
// DOM may have more than one node corresponding to a
|
||||||
// single XPath text node. Coalesce all contiguous text nodes
|
// single XPath text node. Coalesce all contiguous text nodes
|
||||||
// at this level
|
// at this level
|
||||||
for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) {
|
for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) {
|
||||||
result.append(nn.getNodeValue());
|
result.append(nn.getNodeValue());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(outputStream)));
|
||||||
|
result.append(outputStream);
|
||||||
}
|
}
|
||||||
} else {
|
result.append("\n");
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(outputStream)));
|
|
||||||
result.append(outputStream);
|
|
||||||
}
|
}
|
||||||
result.append("\n");
|
return result.toString();
|
||||||
|
} catch (TransformerException e) {
|
||||||
|
return XPathAPI.eval(doc, transform).toString();
|
||||||
}
|
}
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user