BAEL-3219

Parsing an XML File Using SAX Parser
This commit is contained in:
yassin.hajaj 2019-09-01 21:37:01 +02:00 committed by Yassin Hajaj
parent 6f4957a63d
commit 59729b7e23
2 changed files with 18 additions and 82 deletions

View File

@ -111,6 +111,13 @@
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>

View File

@ -1,5 +1,7 @@
package com.baeldung.sax;
import lombok.Data;
import lombok.ToString;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@ -10,7 +12,6 @@ import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class SaxParserMain {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
@ -18,11 +19,17 @@ public class SaxParserMain {
SAXParser saxParser = factory.newSAXParser();
BaeldungHandler baeldungHandler = new BaeldungHandler();
saxParser.parse("xml\\src\\main\\resources\\sax\\baeldung.xml", baeldungHandler);
saxParser.parse("xml/src/main/resources/sax/baeldung.xml", baeldungHandler);
System.out.println(baeldungHandler.getWebsite());
}
@ToString
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;
@ -68,94 +75,16 @@ public class SaxParserMain {
public Baeldung getWebsite() {
return website;
}
@Override
public String toString() {
return "BaeldungHandler{" +
"website=" + website +
", elementValue='" + elementValue + '\'' +
'}';
}
private static final String ARTICLES = "articles";
private static final String ARTICLE = "article";
private static final String TITLE = "title";
private static final String CONTENT = "content";
}
@Data
public static class Baeldung {
private List<BaeldungArticle> articleList;
public List<BaeldungArticle> getArticleList() {
return articleList;
}
public void setArticleList(List<BaeldungArticle> articleList) {
this.articleList = articleList;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Baeldung baeldung = (Baeldung) o;
return Objects.equals(articleList, baeldung.articleList);
}
@Override
public int hashCode() {
return Objects.hash(articleList);
}
@Override
public String toString() {
return "Baeldung{" +
"articleList=" + articleList +
'}';
}
}
@Data
public static class BaeldungArticle {
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaeldungArticle that = (BaeldungArticle) o;
return Objects.equals(title, that.title) &&
Objects.equals(content, that.content);
}
@Override
public int hashCode() {
return Objects.hash(title, content);
}
@Override
public String toString() {
return "BaeldungArticle{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
}