java xsd解析_java dom4j解析XSD文件

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 自带

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();    }   }     } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233272.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • STM32驱动LCD1602程序(调试已成功)「建议收藏」

    STM32驱动LCD1602程序(调试已成功)「建议收藏」IO接线:RSPA8RWPB6EPB7LCD1602_IOPB8-PB15所有的口都配置成开漏输出,并且所有的口都通过10k电阻上拉至5V,部分电路图如下:程序代码LCD1602.C#include”lcd1602.h”voidLCD1602_GPIO_Init_Out()//LCD1602端口初始化,配置为输出{ GPIO_InitTypeDefGPIO_InitStructrue; RCC_APB2PeriphClockCmd(LCD1602_Timer_GPI

    2022年7月16日
    20
  • PMF到底是什么?

    PMF到底是什么?PMF指的是产品与市场匹配的产品关注的数据指标在不同行业、不同业务模式的产品中对应的数值应该是不同的,核心思想在于需要找到一些关键的数据指标,然后通过数据指标来判断产品是否达到了PMF的标准。用户级产品标准·每周使用天数超过3天·初始日新增用户(DNU)超过100·30%新用户次日留存率·达到10万用户量Saas产品标准·…

    2022年5月24日
    70
  • php实现工厂模式

    php实现工厂模式

    2021年11月15日
    53
  • python转置矩阵代码_python 矩阵转置[通俗易懂]

    python转置矩阵代码_python 矩阵转置[通俗易懂]用python怎么实现矩阵的转置只能用循环自己写算法吗自带函数有可以算的吗或者网上的算法可以用的python矩阵转置怎么做?5.矩阵转置给定:L=[[1,2,3],[4,5,6]]用zip函数和列表推导式实现行列转deftranspose(L):T=[list(tpl)fortplinzip(*L)]returnTpython字符串如何变成矩阵进行矩阵转置如输入一串“…

    2022年5月5日
    58
  • mysql下载与安装教程_jdk下载与安装教程

    mysql下载与安装教程_jdk下载与安装教程MySQL下载和安装教程下载MySQL数据库可以访问官方网站:https://www.mysql.com/点击DOWNLOADS模块下的Community模块,进行下载或可访问网页:https://dev.mysql.com/downloads/mysql/直接下载MySQL社区服务版进入,选择MySQL的版本及主机目前最新版本是:MySQLCommunityServer8.0.27,点击下载点击后进入下一页面,该页面可注册MySQL账户,可登录MySQL账户,也可不进行注册,直接下

    2022年9月5日
    6
  • phpstorm64 2021激活码【在线破解激活】[通俗易懂]

    phpstorm64 2021激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号