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 listNotFindName = new HashSet();
public static Set set = new HashSet();//当前type查找出来的所有相关type定义
public static Set setAll = new HashSet();//第一次是全部的 ,removeall()以后是没用的type
//public static Set 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 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 iterator=set.iterator();
Iterator 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 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 xmlMap = new HashMap();
xmlMap.put("xsd", "http://www.w3.org/2001/XMLSchema");
DefaultXPath xpath = new DefaultXPath("//@name|//@type|//@ref|//@base"); xpath.setNamespaceURIs(xmlMap); List elements = xpath.selectNodes(document); for(int i=0;i 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(); } } } }