A memorandum for myself StAX uses either XMLStreamReader or XMLEventReader to read XML. This time I used XMLStreamReader.
Here will be helpful. This was also helpful.
We will borrow the XML of Japanese poems from the here site.
<?xml version="1.0" encoding="UTF-8"?>
<Manyoshu>
<Volume no="1">
<song>
<Song number>8</Song number>
<Original>Jukudatsu 尓 Shipboard Sebu Noboru Tsukiwaiter Shiomo Kana Hinuma Nowadays</Original>
<author>Princess Nukata(Bran's big rice)</author>
<reading>Jukutatsu(Come to)To the ship(Funa)When you get on it, if you wait for a month, the tide will be shining, now rowing(This)Squeeze out(I)Dena</reading>
<image>image/m0008.jpg</image>
<meaning>Jukutatsu(Come to)Then, as I was waiting for the moon to leave the ship, the tide was finally getting better. Now is the time to set sail.</meaning>
</song>
<song>
<Song number>20</Song number>
<Original>Akane Kusashi Takera Maeno died Yukio Nomori Nomoriya Kimiyuki Sleeve cloth style</Original>
<author>Princess Nukata(Bran's big rice)</author>
<reading>madder(Akane)As expected, the standard field bound for Shino(Shimeno)Go, field guard(Nomori)Don't look, you shake your sleeves</reading>
<image>image/m0020.jpg</image>
<meaning>In the field of Emperor Tenji's territory, Shino (full of madder red light), oh, you may shake your sleeves so much that the field guard may see you.</meaning>
</song>
<song>
<Song number>23</Song number>
<Original>Uchima 乎 Mao Ou Shiramizuro Yuya Shooting basket Shimano Pearl algae Masu</Original>
<author>author不明</author>
<reading>Hemp(So), Asa(Mino)King of(Ohokimi), Ama(Ama)Nareya, Ira(Irago)Tamamo on the island(Tamamo)Mow</reading>
<image>image/m0023.jpg</image>
<meaning>Asagu(Mino)King of(Ohokimi)Sama is an ama(Ama)is it,(No, you didn't)You are taking the algae from the island of Irago ...</meaning>
</song>
<song>
<Song number>24</Song number>
<Original>Sora 蝉 yuki Life 乎 Somi Namiyosho Hama Ira Yukino Shimayuki Tamamo-no-Mae</Original>
<author>author不明</author>
<reading>Depressed, spared life, got wet in the waves, Ira(Irago)Tamamo on the island(Tamamo)Mowing(Or)Food(Is)Mu</reading>
<image>image/m0024.jpg</image>
<meaning>Ira, while getting wet with the waves(Irago)Take the algae from the island and eat it ...(Mino)King of(Ohokimi)It is a song that the people of the island sang when they heard the song that they sang with pity when it was sent to the island of Ira.</meaning>
</song>
<song>
<Song number>28</Song number>
<Original>Spring metamorphosis Yoshiyuki Natsurai Shiro Myo Noh Cloth Inui Yu Tennoka Raiyama</Original>
<author>Empress Jito(Jito Tenno)</author>
<reading>It's past spring and summer is coming(Shirotae)Drying clothes and heaven(Rain)Mt. Amanoka(Kaguyama)</reading>
<image>image/m0028.jpg</image>
<meaning>It seems that spring has passed and summer has come. White mystery(Shirotae)The clothes are Kakuyama(Kaguyama)I can see it.</meaning>
</song>
<song>
<Song number>37</Song number>
<Original>Satoshi Shizumi Yoshino Nogawayuki Tokonameno Absolutely Mukyu Revenge</Original>
<author>Kakinomoto no Hitomaro(Hitomaro under the oysters)</author>
<reading>Tokoname in the Yoshino River, which you can see but never get tired of(Tokoname)I look at the edge again without fail</reading>
<image>image/m0037.jpg</image>
<meaning>Tokoname on the Yoshino River that you will never get tired of looking at(Tokoname)Let's go see it again and again without interruption.</meaning>
</song>
</roll>
</Manyoshu>
import java.nio.file.Paths;
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;
public class StAXSample{
public static void main(String args[]) throws Exception {
XMLStreamReader reader = null;
try{
XMLInputFactory factory = XMLInputFactory.newInstance();
reader = factory.createXMLStreamReader(new FileInputStream(Paths.get("waka.xml").toFile()));
String tagName = null;
String content = null;
while(reader.hasNext()){
int eventType = reader.next();
switch(eventType){
case XMLStreamConstants.START_ELEMENT:
tagName = reader.getName().getLocalPart();
break;
case XMLStreamConstants.CHARACTERS:
content = reader.getText();
break;
case XMLStreamConstants.END_ELEMENT:
if(reader.getName().getLocalPart().equals("song")){
break;
}else if(reader.getName().getLocalPart().equals("roll")){
break;
}else if(reader.getName().getLocalPart().equals("Manyoshu")){
break;
}
System.out.println(tagName + " : " + content);
if (reader.getName().getLocalPart().equals("meaning")){
System.out.println("==================================================");
break;
}
default:
break;
}
}
}finally{
if(reader != null){
reader.close();
}
}
}
}
Song number: 8
Original:Jukudatsu 尓 Shipboard Sebu Noboru Tsukiwaiter Shiomo Kana Hinuma Nowadays
author:Princess Nukata(Bran's big rice)
reading:Jukutatsu(Come to)To the ship(Funa)When you get on it, if you wait for a month, the tide will be shining, now rowing(This)Squeeze out(I)Dena
image: image/m0008.jpg
meaning:Jukutatsu(Come to)Then, as I was waiting for the moon to leave the ship, the tide was finally getting better. Now is the time to set sail.
==================================================
Song number: 20
Original:Akane Kusashi Takera Maeno died Yukio Nomori Nomoriya Kimiyuki Sleeve cloth style
author:Princess Nukata(Bran's big rice)
reading:madder(Akane)As expected, the standard field bound for Shino(Shimeno)Go, field guard(Nomori)Don't look, you shake your sleeves
image: image/m0020.jpg
meaning:In the field of Emperor Tenji's territory, Shino (full of madder red light), oh, you may shake your sleeves so much that the field guard may see you.
==================================================
Song number: 23
Original:Uchima 乎 Mao Ou Shiramizuro Yuya Shooting basket Shimano Pearl algae Masu
author:Unknown author
reading:Hemp(So), Asa(Mino)King of(Ohokimi), Ama(Ama)Nareya, Ira(Irago)Tamamo on the island(Tamamo)Mow
image: image/m0023.jpg
meaning:Asagu(Mino)King of(Ohokimi)Sama is an ama(Ama)is it,(No, you didn't)You are taking the algae from the island of Ira.
==================================================
Song number: 24
Original:Sora 蝉 yuki Life 乎 Somi Namiyosho Hama Ira Yukino Shimayuki Tamamo-no-Mae
author:Unknown author
reading:Depressed, spared life, got wet in the waves, Ira(Irago)Tamamo on the island(Tamamo)Mowing(Or)Food(Is)Mu
image: image/m0024.jpg
meaning:Ira, while getting wet with the waves(Irago)Take the algae from the island and eat it ...(Mino)King of(Ohokimi)It is a song that the people of the island sang when they heard the song that they sang with pity when it was sent to the island of Ira.
==================================================
Song number: 28
Original:Spring metamorphosis Yoshiyuki Natsurai Shiro Myo Noh Cloth Inui Yu Tennoka Raiyama
author:Empress Jito(Jito Tenno)
reading:It's past spring and summer is coming(Shirotae)Drying clothes and heaven(Rain)Mt. Amanoka(Kaguyama)
image: image/m0028.jpg
meaning:It seems that spring has passed and summer has come. White mystery(Shirotae)The clothes are Kakuyama(Kaguyama)I can see it.
==================================================
Song number: 37
Original:Satoshi Shizumi Yoshino Nogawayuki Tokonameno Absolutely Mukyu Revenge
author:Kakinomoto no Hitomaro(Hitomaro under the oysters)
reading:Tokoname in the Yoshino River, which you can see but never get tired of(Tokoname)I look at the edge again without fail
image: image/m0037.jpg
meaning:Tokoname on the Yoshino River that you will never get tired of looking at(Tokoname)Let's go see it again and again without interruption.
==================================================
import java.nio.file.Paths;
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;
public class StAXSample{
public static void main(String args[]) throws Exception {
XMLStreamReader reader = null;
try{
//Generate XMLInputFactory
XMLInputFactory factory = XMLInputFactory.newInstance();
//Get the contents of a file in XMLStreamReader
reader = factory.createXMLStreamReader(new FileInputStream(Paths.get("waka.xml").toFile()));
//Variable for print
String tagName = null;
String content = null;
reader = factory.createXMLStreamReader (new FileInputStream (Paths.get ("file.xml "). ToFile ()));
is like a set that is decided by this series of procedures.
To explain briefly
-Specify the file to be read with Paths.get ([filena_name) .toFile ())
. Paths and File compatibility.
-Stream with FileInputStream
-CreateXMLStreamReader
creates an instance of XMLStreamReader, but the argument must be a stream.
//If there is the next event
while(reader.hasNext()){
//Get the type of the following event
int eventType = reader.next();
//Case formula for each event
switch(eventType){
//If it's a start tag
case XMLStreamConstants.START_ELEMENT:
tagName = reader.getName().getLocalPart();
break;
//If it was a text
case XMLStreamConstants.CHARACTERS:
content = reader.getText();
break;
//If it is an end tag
case XMLStreamConstants.END_ELEMENT:
//Conditional branch by element name
if(reader.getName().getLocalPart().equals("song")){
break;
}else if(reader.getName().getLocalPart().equals("roll")){
break;
}else if(reader.getName().getLocalPart().equals("Manyoshu")){
break;
}
System.out.println(tagName + " : " + content);
if (reader.getName().getLocalPart().equals("meaning")){
System.out.println("==================================================");
break;
}
default:
break;
}
}
Basically, the hasNext
method of the XMLInputStreamReader object determines if there is a next event,
If there is, the flow of reading the next event with the next
method`.
}finally{
if(reader != null){
reader.close();
}
}
}
}
Recommended Posts