問題を解決しました。これが、標準の SAX 構文解析に基づく私のアプローチです。
1) カスタム SaxHandler を作成しました。
` public class CustomSAXHandler extends DefaultHandler {
private Stack<XmlNodeInfo> nodeStack = new Stack<XmlNodeInfo>();
private List<String> xPaths;
private XmlNodeInfo rootNode;
private final NamespaceContext namespaceContext;
private List<XmlNodeInfo> resultNodes;
public CustomSAXHandler(String xpath, XmlNodeInfo rootNode, NamespaceContext namespaceContext) {
this.rootNode = rootNode;
this.namespaceContext = namespaceContext;
resultNodes = new ArrayList<XmlNodeInfo>();
xPaths = splitXpaths(xpath);
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
String element = "<" + qName + getAttributes(atts) + ">";
if (!nodeStack.empty()) {
rootNode = nodeStack.peek();
}
if (matchDefinedXpath(qName)) {
XmlNodeInfo newNode = new XmlNodeInfo(qName);
rootNode.addChild(newNode);
nodeStack.push(newNode);
newNode.getHeader().append(element);
} else {
if (!nodeStack.empty()) {
nodeStack.peek().getBody().append(element);
}
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
XmlNodeInfo currentNode = nodeStack.empty() ? null : nodeStack.peek();
if (currentNode != null) {
currentNode.getBody().append(new String(ch, start, length));
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
String finalElement = xPaths.get(xPaths.size() - 1);
String element = "</" + qName + ">";
XmlNodeInfo currentNode = nodeStack.empty() ? null : nodeStack.peek();
if (currentNode != null) {
if (qName.equals(finalElement) && nodeStack.size() == xPaths.size()) {
currentNode.getFooter().append(element);
resultNodes.add(currentNode);
nodeStack.pop();
} else {
if (currentNode.getName().equals(qName)) {
currentNode.getFooter().append(element);
nodeStack.pop();
} else {
currentNode.getBody().append(element);
}
}
}
}
public List<String> getResults() {
List<String> results = new ArrayList<String>();
for (XmlNodeInfo node : resultNodes) {
buildDocument(node, null, results);
}
return results;
}
private void buildDocument(XmlNodeInfo node, String childContent, List<String> results) {
String body = node.getBody().toString();
if (childContent != null) {
body = body + childContent;
}
if (node.getParent() != null && !node.getParent().getName().equals(XmlNodeInfo.ROOT_NODE_NAME)) {
String xmlContent = String.valueOf(node.getHeader()) + body + node.getFooter();
buildDocument(node.getParent(), xmlContent, results);
} else if (node.getParent() != null && node.getParent().getName().equals(XmlNodeInfo.ROOT_NODE_NAME)) {
String finalContent = String.valueOf(node.getHeader()) + body + node.getFooter();
results.add(finalContent);
}
}
private String getAttributes(Attributes atts) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < atts.getLength(); i++) {
String qName = atts.getQName(i);
String value = atts.getValue(qName);
builder.append(" ").append(qName).append("=").append("\"").append(value).append("\"");
}
return builder.toString();
}
private boolean matchDefinedXpath(String nodeName) {
String[] splitWords = nodeName.split(":");
if (splitWords.length == 2) {
String namespacePrefix = splitWords[0];
String namespaceURI = namespaceContext.getNamespaceURI(namespacePrefix);
Iterator prefixes = namespaceContext.getPrefixes(namespaceURI);
while (prefixes.hasNext()) {
String prefix = (String) prefixes.next();
String elementName = prefix + ":" + splitWords[1];
if (xPaths.contains(elementName)) {
return true;
}
}
} else {
return xPaths.contains(nodeName);
}
return false;
}
private List<String> splitXpaths(String xPath) {
if (StringUtils.isNotBlank(xPath)) {
String[] splitWords = xPath.split("/");
if (splitWords.length > 0) {
List<String> results = new ArrayList<String>();
for (String splitWord : splitWords) {
if(StringUtils.isNotBlank(splitWord)){
results.add(splitWord);
}
}
return results;
}
}
return null;
}
}
`
2) ノード データを格納する Bean を作成します。
`
パブリック クラス XmlNodeInfo {
public static final String ROOT_NODE_NAME = "ROOT";
private String name;
private StringBuilder header;
private StringBuilder body;
private StringBuilder footer;
private List<XmlNodeInfo> children;
private XmlNodeInfo parent;
public XmlNodeInfo(String name) {
this.name = name;
header = new StringBuilder();
body = new StringBuilder();
footer = new StringBuilder();
children = new ArrayList<XmlNodeInfo>();
}
public StringBuilder getHeader() {
return header;
}
public StringBuilder getBody() {
return body;
}
public StringBuilder getFooter() {
return footer;
}
public List<XmlNodeInfo> getChildren() {
return children;
}
public void addChild(XmlNodeInfo xmlNodeInfo) {
children.add(xmlNodeInfo);
xmlNodeInfo.setParent(this);
}
public String getName() {
return name;
}
public XmlNodeInfo getParent() {
return parent;
}
public void setParent(XmlNodeInfo parent) {
this.parent = parent;
}
}
`
3) プログラムを実行します。
` public class MainApp {
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
NamespaceContext namespaceContext = new XmlNamespaceResolver();
String xPath = "/CATALOG/MAIN/CD";
InputStream in = MainApp.class.getClassLoader().getResourceAsStream("test.xml");
XmlNodeInfo rootNode = new XmlNodeInfo(XmlNodeInfo.ROOT_NODE_NAME);
CustomSAXHandler customSAXHandler = new CustomSAXHandler(xPath, rootNode, namespaceContext);
saxParser.parse(in, customSAXHandler);
List<String> results = customSAXHandler.getResults(); // result strings
}
}
`
最善の解決策ではないかもしれませんが、私の問題は解決します。お時間とご提案をいただきありがとうございます。