1.用DOM4J解析XSD文件,找出XSD文件中所有的element,type的定义,(xsd文件有4W多行),最终找出的结果是element和type定义有6000多个,
2.递归找出指定type所用到的所有关联的元素,其中有用到XPATH来查找结点
根据type在xsd文件中查找,找到有type和element是自定义的就递归下去继续往下找,直到找到最后所有的type和element都是XSD自带的菜结束
package day3;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.QName;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class Test {
/**
* @param args
* @throws DocumentException
* @throws IOException
* @throws XPathExpressionException
*/
public static Set<String> listNotFindName = new HashSet();
public static Set<String> set = new HashSet();//当前type查找出来的所有相关type定义
public static Set<String> setAll = new HashSet();//第一次是全部的 ,removeall()以后是没用的type
//public static Set<String> settoRemeave = new HashSet();
public static boolean isEnd =false;
public static String beginName="TXLifeResponse";
public static void main(String[] args) throws DocumentException, IOException, XPathExpressionException {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("bbxsd.xml"));
//Document documentAll = saxReader.read(new File("bbxsd.xml"));
Element root = document.getRootElement();
for (Iterator iter = root.elementIterator(); iter.hasNext();)
{
Element e = (Element) iter.next();
System.out.println(e.attributeValue("name"));
setAll.add(e.attributeValue("name"));
}
Map<String, String> xmlMap = new HashMap();
xmlMap.put("xsd", "http://www.w3.org/2001/XMLSchema");
DefaultXPath xpath = new DefaultXPath("/xsd:schema/xsd:simpleType[@name='"+beginName+"']|/xsd:schema/xsd:element[@name='"+beginName+"']|/xsd:schema/xsd:complexType[@name='"+beginName+"']");
xpath.setNamespaceURIs(xmlMap);
Element e =(Element) xpath.selectSingleNode(document);
//System.out.println(e.asXML());
getName(e);
Iterator<String> iterator=set.iterator();
Iterator<String> iterator1=listNotFindName.iterator();
System.out.println("set.size()==="+set.size());
System.out.println("before remove setAll.size()==="+setAll.size());
removeAll();
System.out.println("after remove setAll.size()==="+setAll.size());
System.out.println("listNotFindName.size()==="+listNotFindName.size());
writeSetToFile(setAll,"delete");
writeSetToFile(set,"allsearchType");
writeSetToFile(listNotFindName,"listNotFindName");
}
static Element getElementByName(String name) throws DocumentException
{
//System.out.println("getElementByName(name) name is "+name);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("bbxsd.xml"));
Map<String, String> xmlMap = new HashMap();
xmlMap.put("xsd", "http://www.w3.org/2001/XMLSchema");
DefaultXPath xpath = new DefaultXPath("/xsd:schema/xsd:simpleType[@name='"+beginName+"']|/xsd:schema/xsd:complexType[@name='"+beginName+"']|/xsd:schema/xsd:element[@name='"+beginName+"']");
xpath.setNamespaceURIs(xmlMap);
//Element e = (Element)document.selectSingleNode("/schema/simpleType[@name='"+name+"']|/schema/complexType[@name='"+name+"']|/schema/element[@name='"+name+"']");
//Element e = (Element)document.selectSingleNode("/schema/simpleType[@name='"+name+"' and namespace-uri()='http://www.w3.org/2001/XMLSchema']|/schema/complexType[@name='"+name+"' and namespace-uri()='http://www.w3.org/2001/XMLSchema']|/schema/element[@name='"+name+"' and namespace-uri()='http://www.w3.org/2001/XMLSchema']");
Element e =(Element) xpath.selectSingleNode(document);
//System.out.println("e.elements is "+e.elements().size());
return e;
}
public static void getName(Element e) throws DocumentException{
//System.out.println("begin get name:"+beginName);
if(e==null){
System.out.println("can not find e:"+beginName);
listNotFindName.add(beginName);
set.remove(beginName);
return;
}
String xml = e.asXML();
//System.out.println(xml);
SAXReader saxReader = new SAXReader();
InputSource in = new InputSource(new StringReader(xml));
Document document = saxReader.read(in);
Map<String, String> xmlMap = new HashMap();
xmlMap.put("xsd", "http://www.w3.org/2001/XMLSchema");
DefaultXPath xpath = new DefaultXPath("//@name|//@type|//@ref|//@base");
xpath.setNamespaceURIs(xmlMap);
List<Attribute> elements = xpath.selectNodes(document);
for(int i=0;i<elements.size();i++){
if(elements.get(i).getText().equals(beginName)||elements.get(i).getText().contains("xsd:")||elements.get(i).getText().equals("tc")){
continue;
}
else{
beginName=elements.get(i).getText();
//listName.add(elements.get(i).getText());
//System.out.println("the name is :"+elements.get(i).getText());
if(set.contains(beginName))
continue;
set.add(elements.get(i).getText());
//getElementByName(elements.get(i).getText());
//Element e1 =getElementByName(elements.get(i).getText());
getName(getElementByName(elements.get(i).getText()));
}
}
}
public static void removeAll(){
setAll.removeAll(set);//从全部列表中删除当前找到的就是要找的没用的
}
public static void writeSetToFile(Set set,String filename) throws IOException{
File file = new File(filename);
BufferedWriter wr = new BufferedWriter(new FileWriter(file));
try {
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
//System.out.println(iterator.next());
wr.write(iterator.next());
wr.newLine();
wr.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
wr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}