JDOMのエレメントをフィルタってみた。
JDOMで取得したXMLのElementをフィルターするクラスをつくってみた
…って再利用手段が思い浮かばない(;´∀`)
XMLの中の同じ階層に同じ名前のものが複数あるときに、
アトリビュートでふるいにかけたり、タグでふるいにかけたりしてリストにして返す…と。
フィルターを数回かけたり、特定のタグが持つものだけを抽出したりできます。
いちおうページ構成設定XMLのリスト作成用に作った。
実用的な使用方法が他には思い浮かばないがww
class ElementList { private List<Element> elementList = new ArrayList<Element>(); public ElementList(){} public ElementList(Element element) { this.set(element); } public ElementList(List<Element> elementList) { this.set(elementList); } /* * XMLのエレメントをリストに入れる */ private void toList(Element element) { List<Element> list = new ArrayList<Element>(); Iterator<Element> it = element.getChildren().iterator(); while (it.hasNext()) { list.add(it.next()); } this.set(list); } /* * 特定のタグを持つエレメントのみにする */ public void filter(String tag) { List<Element> elements = this.get(); List<Element> list = new ArrayList<Element>(); Iterator<Element> it = elements.iterator(); while (it.hasNext()) { Element elm = it.next(); if (elm.getName().equals(tag)) { list.add(elm); } } this.set(list); } /* * 特定のアトリビュート値を持つエレメントのみにする */ public void filter(String attribute, String attributeValue, Namespace ns) { List<Element> elements = this.get(); List<Element> list = new ArrayList<Element>(); Iterator<Element> it = elements.iterator(); while (it.hasNext()) { Element elm = it.next(); String elmValue = elm.getAttributeValue(attribute, ns); elmValue = elmValue == null ? "" : elmValue; if (elmValue.equals(attributeValue)) { list.add(elm); } } this.set(list); } public void finder(String tag) { List<Element> list = new ArrayList<Element>(); List<Element> newList = finder(list, tag); this.set(newList); } private List<Element> finder(List<Element> list, String tag) { List<Element> elementList = this.elementList; //指定タグを持つものがあればリストに追加 ElementList elmList = new ElementList(elementList); elmList.filter(tag); if (elmList.isChildren()) { list.addAll(elmList.get()); } //子がいれば孫にフィルタ ElementList childList = new ElementList(elementList); if (childList.isChildren()) { Iterator<Element> it = childList.get().iterator(); while (it.hasNext()) { ElementList grandList = new ElementList(it.next()); grandList.finder(list, tag ); } } return list; } public void finder(String attribute, String attributeValue, Namespace ns) { List<Element> list = new ArrayList<Element>(); List<Element> newList = finder(list, attribute, attributeValue, ns); this.set(newList); } private List<Element> finder(List<Element> list, String attribute, String attributeValue, Namespace ns) { List<Element> elementList = this.elementList; //指定タグを持つものがあればリストに追加 ElementList elmList = new ElementList(elementList); elmList.filter(attribute, attributeValue, ns); if (elmList.isChildren()) { list.addAll(elmList.get()); } //子がいれば孫にフィルタ ElementList childList = new ElementList(elementList); if (childList.isChildren()) { Iterator<Element> it = childList.get().iterator(); while (it.hasNext()) { ElementList grandList = new ElementList(it.next()); grandList.finder(list, attribute, attributeValue, ns); } } return list; } public Element getElement(int i) { Element element = this.get().get(i); return element; } public int length() { int length = this.get().size(); return length; } public boolean isChildren() { return (!this.elementList.isEmpty()); } public List<Element> get() { return elementList; } private void set(List<Element> elementList) { this.elementList = elementList; } private void set(Element element) { this.toList(element); } }