BAEL-3219
Parsing an XML File Using SAX Parser
This commit is contained in:
parent
6f4957a63d
commit
59729b7e23
@ -111,6 +111,13 @@
|
|||||||
<version>${commons-lang.version}</version>
|
<version>${commons-lang.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.8</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.sax;
|
package com.baeldung.sax;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
@ -10,7 +12,6 @@ import javax.xml.parsers.SAXParserFactory;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class SaxParserMain {
|
public class SaxParserMain {
|
||||||
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
|
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
|
||||||
@ -18,11 +19,17 @@ public class SaxParserMain {
|
|||||||
SAXParser saxParser = factory.newSAXParser();
|
SAXParser saxParser = factory.newSAXParser();
|
||||||
|
|
||||||
BaeldungHandler baeldungHandler = new BaeldungHandler();
|
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());
|
System.out.println(baeldungHandler.getWebsite());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ToString
|
||||||
public static class BaeldungHandler extends DefaultHandler {
|
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 Baeldung website;
|
||||||
private String elementValue;
|
private String elementValue;
|
||||||
|
|
||||||
@ -68,94 +75,16 @@ public class SaxParserMain {
|
|||||||
public Baeldung getWebsite() {
|
public Baeldung getWebsite() {
|
||||||
return website;
|
return website;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "BaeldungHandler{" +
|
|
||||||
"website=" + website +
|
|
||||||
", elementValue='" + elementValue + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String ARTICLES = "articles";
|
@Data
|
||||||
private static final String ARTICLE = "article";
|
|
||||||
private static final String TITLE = "title";
|
|
||||||
private static final String CONTENT = "content";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static class Baeldung {
|
public static class Baeldung {
|
||||||
private List<BaeldungArticle> articleList;
|
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 {
|
public static class BaeldungArticle {
|
||||||
private String title;
|
private String title;
|
||||||
private String content;
|
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 + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user