2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

xpathに関するメモ書き

Posted at

xpathに関するメモ書きです。

#ローケーションパス
名称未設定.png

名称未設定 2.png

#関数
名称未設定 4.png

名称未設定 3.png

#サンプル

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

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

// 指定ノードの取得(名前空間無視)
Node targetNode = (Node)xpath.evaluate("//*[local-name()='ProductID']", doc, XPathConstants.NODE);
			
// 指定ノードの取得(名前空間有効)
Node targetNodeWithNS = (Node)xpath.evaluate("//*[local-name()='ProductID' and namespace-uri()='nms:xml.go.jp:schema']", doc, XPathConstants.NODE);
			
// 指定ノードの子ノードリスト
NodeList childNodeList = (NodeList)xpath.evaluate("child::node()", targetNode1, XPathConstants.NODESET);

// 指定ノードの子ノードリスト(簡易表記)
NodeList childNodeList_simple = (NodeList)xpath.evaluate("./node()", targetNode1, XPathConstants.NODESET);

// 指定ノードの親ノード
NodeList childNodeList = (NodeList)xpath.evaluate("parent::node()", targetNode1, XPathConstants.NODESET);

// 指定ノードの親ノード(簡易表記)
NodeList simpleChildNodeList = (NodeList)xpath.evaluate("..", targetNode1, XPathConstants.NODESET);
			
// 祖先ノードリストの取得
NodeList ancestorList = (NodeList)xpath.evaluate("ancestor::node()", targetNode1, XPathConstants.NODESET);
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?