Merge pull request #7639 from pazis/stax

BAEL-3220 Parsing an XML File Using StAX
This commit is contained in:
Erik Pragt 2019-09-01 09:24:23 +10:00 committed by GitHub
commit 5563175efa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,74 @@
package com.baeldung.xml.stax;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ArrayList;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.namespace.QName;
public class StaxParser {
public static List<WebSite> parse(String path) {
List<WebSite> websites = new ArrayList<WebSite>();
WebSite website = null;
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
try {
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
StartElement startElement = nextEvent.asStartElement();
switch (startElement.getName()
.getLocalPart()) {
case "website":
website = new WebSite();
Attribute url = startElement.getAttributeByName(new QName("url"));
if (url != null) {
website.setUrl(url.getValue());
}
break;
case "name":
nextEvent = reader.nextEvent();
website.setName(nextEvent.asCharacters()
.getData());
break;
case "category":
nextEvent = reader.nextEvent();
website.setCategory(nextEvent.asCharacters()
.getData());
break;
case "status":
nextEvent = reader.nextEvent();
website.setStatus(nextEvent.asCharacters()
.getData());
break;
}
}
if (nextEvent.isEndElement()) {
EndElement endElement = nextEvent.asEndElement();
if (endElement.getName()
.getLocalPart()
.equals("website")) {
websites.add(website);
}
}
}
} catch (XMLStreamException xse) {
System.out.println("XMLStreamException");
xse.printStackTrace();
} catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException");
fnfe.printStackTrace();
}
return websites;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.xml.stax;
public class WebSite {
private String url;
private String name;
private String category;
private String status;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.xml.stax;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
public class StaxParserUnitTest {
@Test
public void givenWebsitesXML_whenParsed_thenNotNull() {
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
assertNotNull(websites);
}
@Test
public void givenWebsitesXML_whenParsed_thenSizeIsThree() {
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
assertEquals(3, websites.size());
}
@Test
public void givenWebsitesXML_whenParsed_thenLocalhostExists() {
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
assertEquals("Localhost", websites.get(2).getName());
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<websites>
<website url="https://baeldung.com">
<name>Baeldung</name>
<category>Online Courses</category>
<status>Online</status>
</website>
<website url="http://example.com">
<name>Example</name>
<category>Examples</category>
<status>Offline</status>
</website>
<website url="http://localhost:8080">
<name>Localhost</name>
<category>Tests</category>
<status>Offline</status>
</website>
</websites>