package test.commans.util;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
/**
*map和list的相互嵌套
*同名節(jié)點會放進list中
*/
public class XmlUtils {
/**
* @param args
*/
public static void main(String[] argv) {
sysData();
}
public static void sysData(){
String xmlStr = "<?xml version=\"1.0\"?><?xml-stylesheet type=\"text/xml\" href=\"whatweb.xsl\"?>"
+"<log>"
+"<target>"
+"<uri>http://www.zoomeye.org</uri>"
+"<http-status>200</http-status>"
+"<plugin>"
+"<name>Country</name>"
+"<string>CHINA</string>"
+"<modules>"
+"<module>CN</module>"
+"<module>E</module>"
+"</modules>"
+"</plugin>"
+"<plugin>"
+"<name>HTTPServer</name>"
+"<string>nginx/1.5.2</string>"
+"</plugin>"
+"<plugin>"
+"<name>Email</name>"
+"<string>zoomeye@2x.png</string>"
+"</plugin>"
+"<plugin>"
+"<name>Meta-Refresh-Redirect</name>"
+"<string>/ie</string>"
+"</plugin>"
+"<plugin>"
+"<name>IP</name>"
+"<string>183.60.244.26</string>"
+"</plugin>"
+"<plugin>"
+"<name>Title</name>"
+"<string>ZoomEye | 鐘馗之眼 - 網(wǎng)絡(luò)空間搜索引擎</string>"
+"</plugin>"
+"<plugin>"
+"<name>OpenSearch</name>"
+"<string>/static/opensearch.xml</string>"
+"</plugin>"
+"<plugin>"
+"<name>nginx</name>"
+"<version>1.5.2</version>"
+"</plugin>"
+"<plugin>"
+"<name>Mobile-Website</name>"
+"</plugin>"
+"<plugin>"
+"<name>UncommonHeaders</name>"
+"<string>x-frame-options</string>"
+"</plugin>"
+"<plugin>"
+"<name>X-Frame-Options</name>"
+"<string>SAMEORIGIN</string>"
+"</plugin>"
+"<plugin>"
+"<name>HTML5</name>"
+"</plugin>"
+"<plugin>"
+"<name>X-UA-Compatible</name>"
+"<string>IE=edge</string>"
+"</plugin>"
+"</target>"
// +"<target>"
// ....
// +"</target>"
+"</log>";
try {
StringReader sr = new StringReader(xmlStr.toString().replaceAll("string>", "desc>"));
InputSource is = new InputSource(sr);
Document doc = (new SAXBuilder()).build(is);
// String filterstr = "HTTPServer,Title,X-Frame-Options,UncommonHeaders,Meta-Refresh-Redirect,Country,www.baidu.com,200,X-UA-Compatible,Mobile-Website,IP";
String filterstr = "";
List m = getResultMap(doc,filterstr);
System.out.println(m);
} catch (Exception e) {
e.printStackTrace();
}
}
public static List getResultMap(Document doc,String filterStr){
List maplist = new ArrayList();
if(doc == null)
return maplist;
Element root = doc.getRootElement();
for (Iterator iterator = root.getChildren().iterator(); iterator.hasNext();) {
Map<String, Object> map = new HashMap<String, Object>();
Element e = (Element) iterator.next();
List list = e.getChildren();
if(list.size() > 0){
map.put(e.getName(), getChildMap(e,filterStr));
}else
map.put(e.getName(), e.getText());
maplist.add(map);
}
return maplist;
}
public static Map getChildMap(Element e,String filterStr){
Map map = new HashMap();
List list = e.getChildren();
if(list.size() > 0){
for (int i = 0;i < list.size(); i++) {
Element iter = (Element) list.get(i);
List mapList = new ArrayList();
if(iter.getChildren().size() > 0){
if(validateName(iter,filterStr)){//過濾在filterStr中包含的值
Map m = getChildMap(iter,filterStr);
if(map.get(iter.getName()) != null){
Object obj = map.get(iter.getName());
if(!obj.getClass().getName().equals("java.util.ArrayList")){
mapList = new ArrayList();
mapList.add(obj);
mapList.add(m);
}
if(obj.getClass().getName().equals("java.util.ArrayList")){
mapList = (List) obj;
mapList.add(m);
}
map.put(iter.getName(), mapList);
}else
map.put(iter.getName(), m);
}
}else{
if(validateName(iter,filterStr)){//過濾在filterStr中包含的值
if(map.get(iter.getName()) != null){
Object obj = map.get(iter.getName());
if(!obj.getClass().getName().equals("java.util.ArrayList")){
mapList = new ArrayList();
mapList.add(obj);
mapList.add(iter.getText());
}
if(obj.getClass().getName().equals("java.util.ArrayList")){
mapList = (List) obj;
mapList.add(iter.getText());
}
map.put(iter.getName(), mapList);
}else
map.put(iter.getName(), iter.getText());
}
}
}
}else
map.put(e.getName(), e.getText());
return map;
}
/**判斷含有子節(jié)點的name子節(jié)點的值或單個節(jié)點的值是否在filterContent中存在*/
public static boolean validateName(Element iter,String filterContent){
List children = iter.getChildren();
if(children.size() > 0){
for(int i = 0; i < children.size(); i++){
Element child = (Element) children.get(i);
if("name".equals(child.getName()) && filterContent.contains(child.getText())){
return false;
}
}
}else{
if(filterContent.contains(iter.getText())){
return false;
}
}
return true;
}
}