如果您使用的是 DOM,这很容易。只需遍历文档并跟踪 B 节点即可。命中 E=13 节点时,请删除 B 节点。下面是一些代码来提供帮助:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new File("file.xml"));
DocumentTraversal traversal = (DocumentTraversal) doc;
Node a = doc.getDocumentElement();
NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
Element b = null;
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if ("B".equals(e.getTagName())) {
b = e;
} else if ("E".equals(e.getTagName()) && "13".equals(e.getTextContent()) && b != null) {
a.removeChild(b);
}
}