From a14c19d4e2b70b6cc7a4672919c3601e34b76d5b Mon Sep 17 00:00:00 2001 From: pazis Date: Fri, 23 Aug 2019 21:21:20 +0000 Subject: [PATCH] stax --- .../com/baeldung/xml/stax/StaxParser.java | 67 +++++++++++++++++++ .../java/com/baeldung/xml/stax/WebSite.java | 40 +++++++++++ .../baeldung/xml/stax/StaxParserUnitTest.java | 31 +++++++++ xml/src/test/resources/xml/websites.xml | 18 +++++ 4 files changed, 156 insertions(+) create mode 100644 xml/src/main/java/com/baeldung/xml/stax/StaxParser.java create mode 100644 xml/src/main/java/com/baeldung/xml/stax/WebSite.java create mode 100644 xml/src/test/java/com/baeldung/xml/stax/StaxParserUnitTest.java create mode 100644 xml/src/test/resources/xml/websites.xml diff --git a/xml/src/main/java/com/baeldung/xml/stax/StaxParser.java b/xml/src/main/java/com/baeldung/xml/stax/StaxParser.java new file mode 100644 index 0000000000..6c06bfdb17 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/stax/StaxParser.java @@ -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 parse(String path) { + List webSites = new ArrayList(); + 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; + } + +} diff --git a/xml/src/main/java/com/baeldung/xml/stax/WebSite.java b/xml/src/main/java/com/baeldung/xml/stax/WebSite.java new file mode 100644 index 0000000000..8f7782ab91 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/stax/WebSite.java @@ -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; + } +} diff --git a/xml/src/test/java/com/baeldung/xml/stax/StaxParserUnitTest.java b/xml/src/test/java/com/baeldung/xml/stax/StaxParserUnitTest.java new file mode 100644 index 0000000000..7629487e11 --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/stax/StaxParserUnitTest.java @@ -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 websites = StaxParser.parse("src/test/resources/xml/websites.xml"); + assertNotNull(websites); + } + + @Test + public void givenWebsitesXML_whenParsed_thenSizeIsThree() { + List websites = StaxParser.parse("src/test/resources/xml/websites.xml"); + assertTrue(websites.size() == 3); + } + + @Test + public void givenWebsitesXML_whenParsed_thenLocalhostExists() { + List websites = StaxParser.parse("src/test/resources/xml/websites.xml"); + assertEquals(websites.get(2).getName(),"Localhost"); + } +} diff --git a/xml/src/test/resources/xml/websites.xml b/xml/src/test/resources/xml/websites.xml new file mode 100644 index 0000000000..579c13eb94 --- /dev/null +++ b/xml/src/test/resources/xml/websites.xml @@ -0,0 +1,18 @@ + + + + Baeldung + Online Courses + Online + + + Example + Examples + Offline + + + Localhost + Tests + Offline + + \ No newline at end of file