トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

javaでXpathを使う(1)

Last-modified: 2013-08-24 (土) 02:14:21 (3990d)
Top / javaでXpathを使う(1)

javaでXpathを使う(1)

XMLからautherが「Neal Stephenson」の本のタイトルを取ってくるサンプル

ソース

package example.ex3;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLSample1 {


    private static final String FILENAME="TestFileName.xml";
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ

          // どの Factory インスタンスを使用するのかを表示
          System.out.println(System.getProperty(
          "javax.xml.parsers.DocumentBuilderFactory"));

            try {
              // DocumentBuilderFactory インスタンスを取得
              DocumentBuilderFactory factory =
                  DocumentBuilderFactory.newInstance();

              factory.setNamespaceAware(true); // never forget this!

              // DocumentBuilder を取得
              DocumentBuilder builder =
              factory.newDocumentBuilder();

              // DocumentBuilder を用いて Document オブジェクトを取得
              Document doc =
              builder.parse(new File(FILENAME));

              XPathFactory xPathFactory = XPathFactory.newInstance();
              XPath xpath = xPathFactory.newXPath();

              XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");
              Object result = expr.evaluate(doc, XPathConstants.NODESET);


              NodeList nodes = (NodeList) result;
              for (int i = 0; i < nodes.getLength(); i++) {
                  System.out.println(nodes.item(i).getNodeValue());
              }

            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
          }

}

サンプルXML

<?xml version="1.0" encoding="UTF-8"?>
<inventory>
    <book year="2000">
        <title>Snow Crash</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553380958</isbn>
        <price>14.95</price>
    </book>

    <book year="2005">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <isbn>0743416910</isbn>
        <price>5.99</price>
    </book>

    <book year="1995">
        <title>Zodiac</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553573862</isbn>
        <price>7.50</price>
    </book>

</inventory>

出力

null
Snow Crash
Zodiac