forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaXmlDomCustomFilter.java
More file actions
54 lines (39 loc) · 1.67 KB
/
JavaXmlDomCustomFilter.java
File metadata and controls
54 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.zetcode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
public class JavaXmlDomCustomFilter {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
var factory = DocumentBuilderFactory.newInstance();
var documentBuilder = factory.newDocumentBuilder();
var document = documentBuilder.parse("src/main/resources/continents.xml");
var documentTraversal = (DocumentTraversal) document;
var filter = new MyFilter();
var it = documentTraversal.createNodeIterator(document.getDocumentElement(),
NodeFilter.SHOW_ELEMENT, filter, true);
for (var node = it.nextNode(); node != null; node = it.nextNode()) {
var name = node.getNodeName();
var text = node.getTextContent().trim().replaceAll("\\s+", " ");
System.out.printf("%s: %s%n", name, text);
}
}
static class MyFilter implements NodeFilter {
@Override
public short acceptNode(Node thisNode) {
if (thisNode.getNodeType() == Node.ELEMENT_NODE) {
var e = (Element) thisNode;
var nodeName = e.getNodeName();
if ("slovakia".equals(nodeName) || "poland".equals(nodeName)) {
return NodeFilter.FILTER_ACCEPT;
}
}
return NodeFilter.FILTER_REJECT;
}
}
}