java xsd解析_java dom4j解析XSD文件
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(); ???} ??} ??? } }
總結
以上是生活随笔為你收集整理的java xsd解析_java dom4j解析XSD文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我在富士康13年
- 下一篇: PWM实现语音播放原理