This commit is contained in:
pazis 2019-08-23 21:21:20 +00:00
parent ad2fa55268
commit a14c19d4e2
4 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,67 @@
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");
assertTrue(websites.size() == 3);
}
@Test
public void givenWebsitesXML_whenParsed_thenLocalhostExists() {
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
assertEquals(websites.get(2).getName(),"Localhost");
}
}

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>