Skip to content

Commit 98bce9d

Browse files
committed
2_08_StAX
1 parent 07f595b commit 98bce9d

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import javax.xml.stream.XMLInputFactory;
4+
import javax.xml.stream.XMLStreamException;
5+
import javax.xml.stream.XMLStreamReader;
6+
import javax.xml.stream.events.XMLEvent;
7+
import java.io.InputStream;
8+
9+
public class StaxStreamProcessor implements AutoCloseable {
10+
private static final XMLInputFactory FACTORY = XMLInputFactory.newInstance();
11+
12+
private final XMLStreamReader reader;
13+
14+
public StaxStreamProcessor(InputStream is) throws XMLStreamException {
15+
reader = FACTORY.createXMLStreamReader(is);
16+
}
17+
18+
public XMLStreamReader getReader() {
19+
return reader;
20+
}
21+
22+
public boolean doUntil(int stopEvent, String value) throws XMLStreamException {
23+
while (reader.hasNext()) {
24+
int event = reader.next();
25+
if (event == stopEvent) {
26+
if (value.equals(getValue(event))) {
27+
return true;
28+
}
29+
}
30+
}
31+
return false;
32+
}
33+
34+
public String getValue(int event) throws XMLStreamException {
35+
return (event == XMLEvent.CHARACTERS) ? reader.getText() : reader.getLocalName();
36+
}
37+
38+
public String getElementValue(String element) throws XMLStreamException {
39+
return doUntil(XMLEvent.START_ELEMENT, element) ? reader.getElementText() : null;
40+
}
41+
42+
public String getText() throws XMLStreamException {
43+
return reader.getElementText();
44+
}
45+
46+
@Override
47+
public void close() {
48+
if (reader != null) {
49+
try {
50+
reader.close();
51+
} catch (XMLStreamException e) {
52+
// empty
53+
}
54+
}
55+
}
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import com.google.common.io.Resources;
4+
import org.junit.Test;
5+
6+
import javax.xml.stream.XMLStreamReader;
7+
import javax.xml.stream.events.XMLEvent;
8+
9+
public class StaxStreamProcessorTest {
10+
@Test
11+
public void readCities() throws Exception {
12+
try (StaxStreamProcessor processor =
13+
new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) {
14+
XMLStreamReader reader = processor.getReader();
15+
while (reader.hasNext()) {
16+
int event = reader.next();
17+
if (event == XMLEvent.START_ELEMENT) {
18+
if ("City".equals(reader.getLocalName())) {
19+
System.out.println(reader.getElementText());
20+
}
21+
}
22+
}
23+
}
24+
}
25+
26+
@Test
27+
public void readCities2() throws Exception {
28+
try (StaxStreamProcessor processor =
29+
new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) {
30+
String city;
31+
while ((city = processor.getElementValue("City")) != null) {
32+
System.out.println(city);
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)