JavaでXMLを解析

2014年6月30日

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 

public class T4 {

static class Node{

String nodename;//タグ名前

Map<String, String> attributes;//属性

String text;//テキスト

List<Node> childers;//子要素

@Override
public String toString() {
// TODO Auto-generated method stub

StringBuilder builder=new StringBuilder();

builder.append(“tagname:"+this.nodename);

Iterator<String> iterator=attributes.keySet().iterator();

builder.append(“,attribute:[“);
while(iterator.hasNext()){

String key=iterator.next();
builder.append(key+"="+attributes.get(key)+",");
}
builder.append(“],");
builder.append(“children:[“);
for(Node node:childers){
builder.append(node.nodename+",");
}
builder.append(“]");

return builder.toString().replaceAll(“,]", “]");
}
}

public static void main(String[] args) throws IOException {

String xml="<xml a=b c=d e=5>testxml" +
“<a>testa</a>" +
“<b>testb</b>"+
“</xml>";

Pattern pattern_kt=Pattern.compile(“^<(\\w+)[^<]*>([^<]*)");
Pattern pattern_attr=Pattern.compile(“(\\w+)=(\\w+)");//マッチング属性
Pattern pattern_jw=Pattern.compile(“^</(\\w+)>");//最後文字をマッチング

Pattern pattern=Pattern.compile(“<(\\w+)[^<]*>([^<]*)|</(\\w+)>");
Stack<Node> nodes=new Stack<Node>(); //スタックの要素

List<Node> list_nodes=new ArrayList<Node>();//コレクションの準拠要素

Matcher matcher=pattern.matcher(xml);

while(matcher.find()){

String tmp=matcher.group();
Matcher matcher2=pattern_kt.matcher(tmp);//前頭をマッチング

Matcher matcher3=pattern_jw.matcher(tmp);//最後文字をマッチング

if(matcher2.find()){

Node node=new Node();
//親ノード
if(!nodes.isEmpty()){
nodes.peek().childers.add(node);
}
nodes.push(node);
node.attributes=new HashMap<String, String>();
node.childers=new ArrayList<T4.Node>();

node.nodename=matcher2.group(1);//タグ名前

Matcher attrs=pattern_attr.matcher(tmp);

//属性
while(attrs.find()){

node.attributes.put(attrs.group(1),attrs.group(2));

}
//テキスト
if(matcher2.group(2)!=null){
node.text=matcher2.group(2);
}
}else if(matcher3.find()){
Node node=nodes.pop();
if(node.nodename.equals(matcher3.group(1))){
list_nodes.add(node);
}

}

}

for(Node node:list_nodes){

System.out.println(node);
}

}
}

Development

Posted by arkgame