My DOM (Java)

A memorandum for myself

What is the DOM library used for?

Library for parsing XML

Sample XML used this time

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>(Full of madder red light) In the field of Emperor Tenji's territory, Shino, oh, you shook your sleeves so much,
The field guard may see it.
		</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 ...<br />Asagu(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>

Code sample

Full text


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMSample{
    public static void main(String[] args) throws Exception {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("waka.xml");
        
        Element root = document.getDocumentElement();
        System.out.println("Root element is "+root.getNodeName());
    

        NodeList childNodeList = root.getChildNodes();
        for(int child_i = 0; child_i < childNodeList.getLength(); child_i++){
            Node childNode = childNodeList.item(child_i);
            
            if(childNode.getNodeType() == Node.ELEMENT_NODE){
                System.out.println("Its child element is "+childNode.getNodeName());
                System.out.println("");
                
                NodeList grandchildNodeList = childNode.getChildNodes();
                for(int grachild_i = 0; grachild_i < grandchildNodeList.getLength(); grachild_i++){
                    if(grandchildNodeList.item(grachild_i).getNodeType() == Node.ELEMENT_NODE){

                        NodeList gre_grandchildNodeList = grandchildNodeList.item(grachild_i).getChildNodes();
                        for(int gre_grachild_i = 0; gre_grachild_i < gre_grandchildNodeList.getLength(); gre_grachild_i++){
                            Node node = gre_grandchildNodeList.item(gre_grachild_i);
                            if(node.getNodeType() == Node.ELEMENT_NODE){
                                System.out.println(node.getNodeName() + " : " + node.getTextContent());
                            }
                        }
                        System.out.println("--------------------------------------------------------");
                        
                    }
                }
            }
            
        }

    }
}

Output result

Root element is Manyoshu
Its child element is Volume

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:(Full of madder red light) In the field of Emperor Tenji's territory, Shino, oh, you shook your sleeves so much,
The field guard may see it.

--------------------------------------------------------
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 Irago ...
--------------------------------------------------------
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)I 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.
--------------------------------------------------------

Partial explanation of the code

Library import

//Library for reading documents
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//Library for document manipulation
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Load document

public class DOMSample{
    public static void main(String[] args) throws Exception{
        //Document reading
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("waka.xml")

It feels like a standard sentence here

Get root element

        //Get root element
        Element root = document.getDocumentElement();
        //Show node name of root node
        System.out.println(root.getNodeName());

It seems that you can get the top layer element from the current position with the getDocumentElement method. Then, get the element name of the node obtained by the getNodeName method.

By the way, you can also get the attribute value with the getAttribute () method.

Get child elements

        //Get child node
        NodeList childNodeList = root.getChildNodes();
        //Loop on the acquired child node
        for(int child_i = 0; child_i < childNodeList.getLength(); child_i++){
            Node childNode = childNodeList.item(child_i);
            
            //If the child node is an element node
            if(childNode.getNodeType() == Node.ELEMENT_NODE){
                //Get the node name of a child node
                System.out.println("Its child element is "+childNode.getNodeName());
                System.out.println("");

Get the child elements of that element with the getChildNodes method. Determine the type of the node with getNodeType See here for node types (https://www.techscore.com/tech/Java/JavaSE/DOM/2-3/)

Getting grandchildren and great-grandchildren

            //Get grandchild node
            NodeList grandchildNodeList = childNode.getChildNodes();
                //Loop inside the grandchild node
                for(int grachild_i = 0; grachild_i < grandchildNodeList.getLength(); grachild_i++){
                    //If the grandchild node is an element node
                    if(grandchildNodeList.item(grachild_i).getNodeType()==Node.ELEMENT_NODE){
                        //Get great-grandchild node from grandchild node
                        NodeList gre_grandchildNodeList = grandchildNodeList.item(grachild_i).getChildNodes();

                        //Loop on great-grandson node
                        for(int gre_grachild_i = 0; gre_grachild_i < gre_grandchildNodeList.getLength(); gre_grachild_i++){
                            
                            Node node = gre_grandchildNodeList.item(gre_grachild_i);
                            //If the node is an element node, display the node name and contents
                            if(node.getNodeType() == Node.ELEMENT_NODE){
                                System.out.println(node.getNodeName() + " : " + node.getTextContent());
                            }
                        }
                        System.out.println("--------------------------------------------------------");


You can display the contents of that element with the getTextContent method.

Recommended Posts

My DOM (Java)
My StAX (Java)
My Java reference
My DAO pattern (Java)
My Study Note (Java)
Java
Java
My note: Introducing Java to Ubuntu
Java learning (0)
My thoughts on the equals method (Java)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner