日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java xsd解析_java dom4j解析XSD文件

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。