[Java8]xmlファイルから要素名を指定して値を取得する方法

環境
JavaSE 1.8
Eclipse 4.14.0

構文
1.newInstance()
DocumentBuilderFactoryの新しいインスタンスを取得します。
このstaticメソッドは新しいファクトリ・インスタンスを作成します。
2.newDocumentBuilder()
現在構成されているパラメータを使用してDocumentBuilderの新しいインスタンスを作成します。

3.getElementsByTagName(String tagname)
文書内の、特定のタグ名を持つすべてのElementsを文書の順に格納するNodeListを返します。
パラメータ:
tagname – 一致するタグの名前。特殊な値「*」はすべてのタグに一致する。
XMLの場合、tagnameパラメータでは大文字と小文字が区別される。

使用例
1.test.xml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0" encoding="UTF-8" ?>
<person>
<User>
<name>
<city> 東京</city>
<city> 大阪</city>
<city> 福岡</city>
</name>
</User>
</person>
<?xml version="1.0" encoding="UTF-8" ?> <person> <User> <name> <city> 東京</city> <city> 大阪</city> <city> 福岡</city> </name> </User> </person>
<?xml version="1.0" encoding="UTF-8" ?>
<person>
      <User>
            <name>
                  <city> 東京</city>
                  <city> 大阪</city>
                  <city> 福岡</city>
            </name>
      </User>
</person>

2.Java側のプログラム

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlGet {
private static final String xmlFile = "C:\\study\\arkgame\\test.xml";
public static void main(String[] args) throws Exception {
// DOMの変数宣言
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// XML文書を読み込む
Document doc = db.parse(new FileInputStream(xmlFile));
// 要素をノードリストとして取り出す
NodeList nodeLst = doc.getElementsByTagName("city");
for (int i = 0; i < nodeLst.getLength(); i++) {
Node count = nodeLst.item(i);
for (Node ele = count.getFirstChild(); ele != null; ele = ele.getNextSibling()) {
System.out.println("city要素" + (i+1) + " 値:" + ele.getNodeValue());
}
}
}
}
package com.arkgame.study; import java.io.FileInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XmlGet { private static final String xmlFile = "C:\\study\\arkgame\\test.xml"; public static void main(String[] args) throws Exception { // DOMの変数宣言 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // XML文書を読み込む Document doc = db.parse(new FileInputStream(xmlFile)); // 要素をノードリストとして取り出す NodeList nodeLst = doc.getElementsByTagName("city"); for (int i = 0; i < nodeLst.getLength(); i++) { Node count = nodeLst.item(i); for (Node ele = count.getFirstChild(); ele != null; ele = ele.getNextSibling()) { System.out.println("city要素" + (i+1) + " 値:" + ele.getNodeValue()); } } } }
package com.arkgame.study;

import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlGet {

      private static final String xmlFile = "C:\\study\\arkgame\\test.xml";

      public static void main(String[] args) throws Exception {
            // DOMの変数宣言
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            
            // XML文書を読み込む
            Document doc = db.parse(new FileInputStream(xmlFile));
            
            // 要素をノードリストとして取り出す
            NodeList nodeLst = doc.getElementsByTagName("city");

            for (int i = 0; i < nodeLst.getLength(); i++) {
                  Node count = nodeLst.item(i);
                  for (Node ele = count.getFirstChild(); ele != null; ele = ele.getNextSibling()) {
                        System.out.println("city要素" + (i+1) + " 値:" + ele.getNodeValue());
                  }
            }
      }
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
city要素1 値: 東京
city要素2 値: 大阪
city要素3 値: 福岡
city要素1 値: 東京 city要素2 値: 大阪 city要素3 値: 福岡
city要素1 値: 東京
city要素2 値: 大阪
city要素3 値: 福岡

 

Java

Posted by arkgame