Skip to content

Instantly share code, notes, and snippets.

@johobemax
Created August 26, 2010 16:48
Show Gist options
  • Save johobemax/551748 to your computer and use it in GitHub Desktop.
Save johobemax/551748 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<addressbook>
<person>
<name>マックス太郎</name>
<address>
<prefecture>岡山県</prefecture>
<city>岡山市北区</city>
<block>島田本町1-6-12</block>
</address>
</person>
<person>
<name>マックス次郎</name>
<address>
<prefecture>岡山県</prefecture>
<city>倉敷市</city>
<block>老松町xxxxx</block>
</address>
</person>
<person>
<name>マックス三郎</name>
<address>
<prefecture>岡山県</prefecture>
<town>久米郡美咲町</town>
<block>柵原xxxxx</block>
</address>
</person>
<person>
<name>マックス四朗</name>
<address>
<prefecture>広島県</prefecture>
<town>世羅郡世羅町</town>
<block>西上原xxxxx</block>
</address>
</person>
<person>
<name>マックス五郎</name>
<address>
<prefecture>岡山県</prefecture>
<city>岡山市北区</city>
<block>駅元町5-11</block>
</address>
</person>
</addressbook>
package xmldomtest;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.ArrayList;
public class JavaDomSample {
public static void main(String[] args) {
String name = null;
ArrayList<String> resultList = new ArrayList<String>();
String[] result = null;
try{
File xml = new File("addressbook.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xml);
NodeList persons = doc.getElementsByTagName("person");
for(int i=0; i<persons.getLength(); i++){
NodeList ch = persons.item(i).getChildNodes();
for(int j=0; j<ch.getLength(); j++){
Node n = ch.item(j);
if(n.getNodeType()==Node.ELEMENT_NODE){
if(n.getNodeName().equals("name")){
name = n.getFirstChild().getNodeValue();
}
if(n.getNodeName().equals("address")){
NodeList ch2 = n.getChildNodes();
for(int k=0; k<ch2.getLength(); k++){
Node n2 = ch2.item(k);
if(n2.getNodeType()==Node.ELEMENT_NODE){
if(n2.getNodeName().equals("city")){
if(n2.getFirstChild().getNodeValue().equals("岡山市北区")){
resultList.add(name);
}
}
}
}
}
}
}
}
result = resultList.toArray(new String[resultList.size()]);
}catch(Exception e){
e.printStackTrace();
result = null;
}
for(String s:result){
System.out.println(s);
}
}
}
package xmlxpathtest;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class JavaXPathSample {
public static void main(String[] args) {
String[] result = null;
try{
File xml = new File("addressbook.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xml);
XPathFactory xfact = XPathFactory.newInstance();
XPath xp = xfact.newXPath();
XPathExpression xpex = xp.compile("/addressbook/person/name[../address/city/text()='岡山市北区']/text()");
Object res = xpex.evaluate(doc, XPathConstants.NODESET);
NodeList resultList = (NodeList)res;
result = new String[resultList.getLength()];
for(int i=0; i<resultList.getLength(); i++){
result[i] = resultList.item(i).getNodeValue();
}
}catch(Exception e){
e.printStackTrace();
result = null;
}
for(String s:result){
System.out.println(s);
}
}
}
require 'rexml/document'
include 'REXML'
File.open("addressbook.xml") do |f|
doc = Document.new f
result = XPath.match doc, "/addressbook/person/name[../address/city/text()='岡山市北区']/text()"
result.each do |name|
puts name
end
end
@johobemax
Copy link
Author

adressbook.xmlから、住所が「岡山市北区」である人の名前を配列 result に格納し、全要素を表示するプログラム。

JavaDomSample.java → Java言語のorg.w3c.domパッケージを使用したバージョン。ソースが複雑で、何をやっているのかとても分かりづらい。
JavaXPathSample.java → Java言語のjavax.xml.xpathパッケージを使用したバージョン。XPathの検索機能を利用して簡単に目的のデータを取得できる。が、コードはまだまだ分かりづらい。Java5以降に標準装備。
RubyRexmlSample.rb → Ruby言語の標準XMLライブラリを使用したバージョン。XPathを利用して簡単に目的のデータを取得できる。さらに、ソースが簡潔で分かりやすい。

※調べれば、Javaにも簡潔に処理できるライブラリがあるかもしれない。

ちなみに、いずれも

マックス太郎
マックス五郎

と表示されます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment