stax fix
This commit is contained in:
parent
a14c19d4e2
commit
0eedbaa5f4
|
@ -1,4 +1,5 @@
|
|||
package com.baeldung.xml.stax;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.List;
|
||||
|
@ -16,52 +17,58 @@ import javax.xml.namespace.QName;
|
|||
public class StaxParser {
|
||||
|
||||
public static List<WebSite> parse(String path) {
|
||||
List<WebSite> webSites = new ArrayList<WebSite>();
|
||||
WebSite webSite = null;
|
||||
List<WebSite> websites = new ArrayList<WebSite>();
|
||||
WebSite website = null;
|
||||
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
|
||||
try {
|
||||
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
|
||||
while(reader.hasNext()){
|
||||
while (reader.hasNext()) {
|
||||
XMLEvent nextEvent = reader.nextEvent();
|
||||
if (nextEvent.isStartElement()){
|
||||
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;
|
||||
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()){
|
||||
if (nextEvent.isEndElement()) {
|
||||
EndElement endElement = nextEvent.asEndElement();
|
||||
if(endElement.getName().getLocalPart().equals("website")){
|
||||
webSites.add(webSite);
|
||||
if (endElement.getName()
|
||||
.getLocalPart()
|
||||
.equals("website")) {
|
||||
websites.add(website);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException xse){
|
||||
} catch (XMLStreamException xse) {
|
||||
System.out.println("XMLStreamException");
|
||||
xse.printStackTrace();
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
System.out.println("FileNotFoundException");
|
||||
fnfe.printStackTrace();
|
||||
}
|
||||
return webSites;
|
||||
return websites;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,12 +20,12 @@ public class StaxParserUnitTest {
|
|||
@Test
|
||||
public void givenWebsitesXML_whenParsed_thenSizeIsThree() {
|
||||
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
|
||||
assertTrue(websites.size() == 3);
|
||||
assertEquals(3, websites.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWebsitesXML_whenParsed_thenLocalhostExists() {
|
||||
List<WebSite> websites = StaxParser.parse("src/test/resources/xml/websites.xml");
|
||||
assertEquals(websites.get(2).getName(),"Localhost");
|
||||
assertEquals("Localhost", websites.get(2).getName());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue