Merge pull request #7680 from YassinHajaj/master

[BAEL-3219] - Parsing an XML File Using SAX Parser
This commit is contained in:
rpvilao 2019-09-28 17:27:59 +02:00 committed by GitHub
commit 0b834830b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,109 @@
package com.baeldung.sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SaxParserMain {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
BaeldungHandler baeldungHandler = new BaeldungHandler();
saxParser.parse("xml/src/main/resources/sax/baeldung.xml", baeldungHandler);
System.out.println(baeldungHandler.getWebsite());
}
public static class BaeldungHandler extends DefaultHandler {
private static final String ARTICLES = "articles";
private static final String ARTICLE = "article";
private static final String TITLE = "title";
private static final String CONTENT = "content";
private Baeldung website;
private String elementValue;
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
elementValue = new String(ch, start, length);
}
@Override
public void startDocument() throws SAXException {
website = new Baeldung();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case ARTICLES:
website.setArticleList(new ArrayList<>());
break;
case ARTICLE:
website.getArticleList().add(new BaeldungArticle());
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case TITLE:
latestArticle().setTitle(elementValue);
break;
case CONTENT:
latestArticle().setContent(elementValue);
break;
}
}
private BaeldungArticle latestArticle() {
List<BaeldungArticle> articleList = website.getArticleList();
int latestArticleIndex = articleList.size() - 1;
return articleList.get(latestArticleIndex);
}
public Baeldung getWebsite() {
return website;
}
}
public static class Baeldung {
private List<BaeldungArticle> articleList;
public void setArticleList(List<BaeldungArticle> articleList) {
this.articleList = articleList;
}
public List<BaeldungArticle> getArticleList() {
return this.articleList;
}
}
public static class BaeldungArticle {
private String title;
private String content;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return this.content;
}
}
}

View File

@ -0,0 +1,16 @@
<baeldung>
<articles>
<article>
<title>Parsing an XML File Using SAX Parser</title>
<content>SAX Parser's Lorem ipsum...</content>
</article>
<article>
<title>Parsing an XML File Using DOM Parser</title>
<content>DOM Parser's Lorem ipsum...</content>
</article>
<article>
<title>Parsing an XML File Using StAX Parser</title>
<content>StAX Parser's Lorem ipsum...</content>
</article>
</articles>
</baeldung>

View File

@ -0,0 +1,44 @@
package com.baeldung.sax;
import org.junit.Test;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.*;
public class SaxParserMainUnitTest {
@Test
public void givenAProperXMLFile_whenItIsParsed_ThenAnObjectContainsAllItsElements() throws IOException, SAXException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
SaxParserMain.BaeldungHandler baeldungHandler = new SaxParserMain.BaeldungHandler();
saxParser.parse("src/test/resources/sax/baeldung.xml", baeldungHandler);
SaxParserMain.Baeldung result = baeldungHandler.getWebsite();
assertNotNull(result);
List<SaxParserMain.BaeldungArticle> articles = result.getArticleList();
assertNotNull(articles);
assertEquals(3, articles.size());
SaxParserMain.BaeldungArticle articleOne = articles.get(0);
assertEquals("Parsing an XML File Using SAX Parser", articleOne.getTitle());
assertEquals("SAX Parser's Lorem ipsum...", articleOne.getContent());
SaxParserMain.BaeldungArticle articleTwo = articles.get(1);
assertEquals("Parsing an XML File Using DOM Parser", articleTwo.getTitle());
assertEquals("DOM Parser's Lorem ipsum...", articleTwo.getContent());
SaxParserMain.BaeldungArticle articleThree = articles.get(2);
assertEquals("Parsing an XML File Using StAX Parser", articleThree.getTitle());
assertEquals("StAX Parser's Lorem ipsum...", articleThree.getContent());
}
}

View File

@ -0,0 +1,16 @@
<baeldung>
<articles>
<article>
<title>Parsing an XML File Using SAX Parser</title>
<content>SAX Parser's Lorem ipsum...</content>
</article>
<article>
<title>Parsing an XML File Using DOM Parser</title>
<content>DOM Parser's Lorem ipsum...</content>
</article>
<article>
<title>Parsing an XML File Using StAX Parser</title>
<content>StAX Parser's Lorem ipsum...</content>
</article>
</articles>
</baeldung>