在 Java 中使用 XSOM 解析 XSD 架构。如何访问元素和复杂类型

发布于 2024-12-12 22:20:15 字数 5057 浏览 2 评论 0原文

我在 Java 中使用 XSOM 解析 .XSD 文件时遇到了很多困难。我有两个 .XSD 文件,一个定义日历,第二个定义全局类型。我希望能够读取日历文件并确定:

日历有 3 个属性

  • Valid 是一个名为 eYN 的 ENUM
  • Cal 是一个字符串
  • Status 是一个名为 eSTATUS

Calendar.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:gtypes="http://www.btec.com/gtypes"
 elementFormDefault="qualified">
<xs:import namespace="http://www.btec.com/gtypes"
 schemaLocation="gtypes.xsd"/>
<xs:element name="CALENDAR">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Valid" type="eYN" minOccurs="0"/>
      <xs:element name="Cal" minOccurs="0">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="gtypes:STRING">
              <xs:attribute name="IsKey" type="xs:string" fixed="Y"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="Status" type="eSTATUS" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:complexType name="eSTATUS">
  <xs:simpleContent>
    <xs:extension base="gtypes:ENUM"/>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="eYN">
  <xs:simpleContent>
    <xs:extension base="gtypes:ENUM"/>
  </xs:simpleContent>
</xs:complexType>

gtypes .xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.btec.com/gtypes"
 elementFormDefault="qualified">
<xs:complexType name="ENUM">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="TYPE" fixed="ENUM"/>
      <xs:attribute name="derived" use="optional"/>
      <xs:attribute name="readonly" use="optional"/>
      <xs:attribute name="required" use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="STRING">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="TYPE" use="optional"/>
      <xs:attribute name="derived" use="optional"/>
      <xs:attribute name="readonly" use="optional"/>
      <xs:attribute name="required" use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:schema>

我尝试访问此信息的代码如下。我对 Java 还很陌生,所以 欢迎任何风格批评。

我真的需要知道

  1. 如何访问复杂类型 cal 并查看它是一个字符串?
  2. 如何访问 Status 的定义以查看它是 eSTATUS 类型的枚举 强调文本

我已经多次尝试通过 ComplexType 以及 Elements and Content 访问正确的信息。然而我只是不明白,我找不到任何有帮助的例子。我期望(希望)当你知道如何做时,最好的方法是(相对)简单的。所以,再一次,如果有人能指出我正确的方向,那将是一个很大的帮助。

xmlfile = "Calendar.xsd"
XSOMParser parser = new XSOMParser();

parser.parse(new File(xmlfile));
XSSchemaSet sset = parser.getResult();
XSSchema s = sset.getSchema(1);
if (s.getTargetNamespace().equals("")) // this is the ns with all the stuff
       // in
{
  // try ElementDecls
  Iterator jtr = s.iterateElementDecls();
  while (jtr.hasNext())
  {
    XSElementDecl e = (XSElementDecl) jtr.next();
    System.out.print("got ElementDecls " + e.getName());
    // ok we've got a CALENDAR.. what next?
    // not this anyway
    /*  
     *
     * XSParticle[] particles = e.asElementDecl() for (final XSParticle p :
     * particles) { final XSTerm pterm = p.getTerm(); if
     * (pterm.isElementDecl()) { final XSElementDecl ed =
     * pterm.asElementDecl(); System.out.println(ed.getName()); }
     */
  }

  // try all Complex Types in schema
  Iterator<XSComplexType> ctiter = s.iterateComplexTypes();
  while (ctiter.hasNext())
  {
    // this will be a eSTATUS. Lets type and get the extension to 
    // see its a ENUM
    XSComplexType ct = (XSComplexType) ctiter.next();
    String typeName = ct.getName();
    System.out.println(typeName + newline);

    // as Content
    XSContentType content = ct.getContentType();
    // now what?
    // as Partacle?
    XSParticle p2 = content.asParticle();
    if (null != p2)
    {
      System.out.print("We got partical thing !" + newline);
      // might would be good if we got here but we never do :-(
    }

    // try complex type Element Decs
    List<XSElementDecl> el = ct.getElementDecls();
    for (XSElementDecl ed : el)
    {
      System.out.print("We got ElementDecl !" + ed.getName() + newline);
      // would be good if we got here but we never do :-(
    }

    Collection<? extends XSAttributeUse> c = ct.getAttributeUses();
    Iterator<? extends XSAttributeUse> i = c.iterator();
    while (i.hasNext())
    {
      XSAttributeDecl attributeDecl = i.next().getDecl();
      System.out.println("type: " + attributeDecl.getType());
      System.out.println("name:" + attributeDecl.getName());
    }
  }
}

I’m having a lot of difficuly parsing an .XSD file with a XSOM in Java. I have two .XSD files one defines a calendar and the second the global types. I'd like to be able to read the calendar file and determine that:

calendar has 3 properties

  • Valid is an ENUM called eYN
  • Cal is a String
  • Status is a ENUM called eSTATUS

Calendar.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:gtypes="http://www.btec.com/gtypes"
 elementFormDefault="qualified">
<xs:import namespace="http://www.btec.com/gtypes"
 schemaLocation="gtypes.xsd"/>
<xs:element name="CALENDAR">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Valid" type="eYN" minOccurs="0"/>
      <xs:element name="Cal" minOccurs="0">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="gtypes:STRING">
              <xs:attribute name="IsKey" type="xs:string" fixed="Y"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="Status" type="eSTATUS" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:complexType name="eSTATUS">
  <xs:simpleContent>
    <xs:extension base="gtypes:ENUM"/>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="eYN">
  <xs:simpleContent>
    <xs:extension base="gtypes:ENUM"/>
  </xs:simpleContent>
</xs:complexType>

gtypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.btec.com/gtypes"
 elementFormDefault="qualified">
<xs:complexType name="ENUM">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="TYPE" fixed="ENUM"/>
      <xs:attribute name="derived" use="optional"/>
      <xs:attribute name="readonly" use="optional"/>
      <xs:attribute name="required" use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
<xs:complexType name="STRING">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="TYPE" use="optional"/>
      <xs:attribute name="derived" use="optional"/>
      <xs:attribute name="readonly" use="optional"/>
      <xs:attribute name="required" use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:schema>

The code from my attempt to access this information is below. I'm pretty new to Java so
any style criticism welcome.

I really need to know

  1. How to I access the complex type cal and see that it's a string?
  2. How do I access the definition of Status to see it's a enumeration of type eSTATUS
    emphasized text

I've has several attempts to access the right information via ComplexType and Elements and Content. However I'm just don't get it and I cannot find any examples that help. I expect (hope) the best method is (relatively) simple when you know how. So, once again, if anyone could point me in the right direction that would be a great help.

xmlfile = "Calendar.xsd"
XSOMParser parser = new XSOMParser();

parser.parse(new File(xmlfile));
XSSchemaSet sset = parser.getResult();
XSSchema s = sset.getSchema(1);
if (s.getTargetNamespace().equals("")) // this is the ns with all the stuff
       // in
{
  // try ElementDecls
  Iterator jtr = s.iterateElementDecls();
  while (jtr.hasNext())
  {
    XSElementDecl e = (XSElementDecl) jtr.next();
    System.out.print("got ElementDecls " + e.getName());
    // ok we've got a CALENDAR.. what next?
    // not this anyway
    /*  
     *
     * XSParticle[] particles = e.asElementDecl() for (final XSParticle p :
     * particles) { final XSTerm pterm = p.getTerm(); if
     * (pterm.isElementDecl()) { final XSElementDecl ed =
     * pterm.asElementDecl(); System.out.println(ed.getName()); }
     */
  }

  // try all Complex Types in schema
  Iterator<XSComplexType> ctiter = s.iterateComplexTypes();
  while (ctiter.hasNext())
  {
    // this will be a eSTATUS. Lets type and get the extension to 
    // see its a ENUM
    XSComplexType ct = (XSComplexType) ctiter.next();
    String typeName = ct.getName();
    System.out.println(typeName + newline);

    // as Content
    XSContentType content = ct.getContentType();
    // now what?
    // as Partacle?
    XSParticle p2 = content.asParticle();
    if (null != p2)
    {
      System.out.print("We got partical thing !" + newline);
      // might would be good if we got here but we never do :-(
    }

    // try complex type Element Decs
    List<XSElementDecl> el = ct.getElementDecls();
    for (XSElementDecl ed : el)
    {
      System.out.print("We got ElementDecl !" + ed.getName() + newline);
      // would be good if we got here but we never do :-(
    }

    Collection<? extends XSAttributeUse> c = ct.getAttributeUses();
    Iterator<? extends XSAttributeUse> i = c.iterator();
    while (i.hasNext())
    {
      XSAttributeDecl attributeDecl = i.next().getDecl();
      System.out.println("type: " + attributeDecl.getType());
      System.out.println("name:" + attributeDecl.getName());
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-12-19 22:20:15

经过大量谷歌搜索后,我想我已经回答了我自己的问题。我提出的解决方案完全偏离了目标。
主要问题是 XSD 有三个命名空间,而我在错误的命名空间中查找了错误的内容。

如果您希望在 XSOM 中解析 XSD,请确保您在开始之前了解 XSD 的结构以及标签的含义 - 这将为您节省大量时间。

我将在下面发布我的版本,因为我确信它可以改进!

一些有用的链接:

http://msdn.microsoft.com/en-us /library/ms187822.aspx

http://it.toolbox.com /blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565

http://www.w3schools.com/schema/el_simpleContent.asp

package xsom.test

import com.sun.xml.xsom.parser.XSOMParser;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSContentType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSModelGroup;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.XSTerm;

import java.util.Iterator;
import java.io.File;
import java.util.HashMap;

public class mappingGenerator
{
  private HashMap mappings;

  public mappingGenerator()
  {
    mappings = new HashMap();
  }

  public void generate(String xmlfile) throws Exception
  {

    // with help from
    // http://msdn.microsoft.com/en-us/library/ms187822.aspx
    // http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565
    // http://www.w3schools.com/schema/el_simpleContent.asp
    XSOMParser parser = new XSOMParser();

    parser.parse(new File(xmlfile));
    XSSchemaSet sset = parser.getResult();

    // =========================================================
    // types namepace
    XSSchema gtypesSchema = sset.getSchema("http://www.btec.com/gtypes");
    Iterator<XSComplexType> ctiter = gtypesSchema.iterateComplexTypes();
    while (ctiter.hasNext())
    {
      XSComplexType ct = (XSComplexType) ctiter.next();
      String typeName = ct.getName();
      // these are extensions so look at the base type to see what it is
      String baseTypeName = ct.getBaseType().getName();
      System.out.println(typeName + " is a " + baseTypeName);
    }

    // =========================================================
    // global namespace
    XSSchema globalSchema = sset.getSchema("");
    // local definitions of enums are in complex types
    ctiter = globalSchema.iterateComplexTypes();
    while (ctiter.hasNext())
    {
      XSComplexType ct = (XSComplexType) ctiter.next();
      String typeName = ct.getName();
      String baseTypeName = ct.getBaseType().getName();
      System.out.println(typeName + " is a " + baseTypeName);
    }

    // =========================================================
    // the main entity of this file is in the Elements
    // there should only be one!
    if (globalSchema.getElementDecls().size() != 1)
    {
      throw new Exception("Should be only elment type per file.");
    }

    XSElementDecl ed = globalSchema.getElementDecls().values()
        .toArray(new XSElementDecl[0])[0];
    String entityType = ed.getName();
    XSContentType xsContentType = ed.getType().asComplexType().getContentType();
    XSParticle particle = xsContentType.asParticle();
    if (particle != null)
    {

      XSTerm term = particle.getTerm();
      if (term.isModelGroup())
      {
        XSModelGroup xsModelGroup = term.asModelGroup();
        term.asElementDecl();
        XSParticle[] particles = xsModelGroup.getChildren();
        String propertyName = null;
        String propertyType = null;
        XSParticle pp =particles[0];
        for (XSParticle p : particles)
        {
          XSTerm pterm = p.getTerm();
          if (pterm.isElementDecl())
          {            
            propertyName = pterm.asElementDecl().getName();
            if (pterm.asElementDecl().getType().getName() == null)
            {
              propertyType = pterm.asElementDecl().getType().getBaseType().getName();
            }
            else
            {
              propertyType = pterm.asElementDecl().getType().getName();              
            }
            System.out.println(propertyName + " is a " + propertyType);
          }
        }
      }
    }
    return;
  }
}   

的输出是:

ENUM is a string
STRING is a string
eSTATUS is a ENUM
eYN is a ENUM
Valid is a eYN
Cal is a STRING
Status is a eSTATUS

Well after a lot googling I think I've answered my own question. My proposed solution was hopelessly wide of the mark.
The main problem was that the XSD has three namespaces and I was looking in the wrong one for the wrong thing.

If you're looking to parse an XSD in XSOM be sure you understand the structure of the XSD and what the tags mean before you start - it will save you a lot of time.

I'll post my version below as I'm sure it can be improved!

Some links that were helpful:

http://msdn.microsoft.com/en-us/library/ms187822.aspx

http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565

http://www.w3schools.com/schema/el_simpleContent.asp

package xsom.test

import com.sun.xml.xsom.parser.XSOMParser;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSContentType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSModelGroup;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.XSTerm;

import java.util.Iterator;
import java.io.File;
import java.util.HashMap;

public class mappingGenerator
{
  private HashMap mappings;

  public mappingGenerator()
  {
    mappings = new HashMap();
  }

  public void generate(String xmlfile) throws Exception
  {

    // with help from
    // http://msdn.microsoft.com/en-us/library/ms187822.aspx
    // http://it.toolbox.com/blogs/enterprise-web-solutions/parsing-an-xsd-schema-in-java-32565
    // http://www.w3schools.com/schema/el_simpleContent.asp
    XSOMParser parser = new XSOMParser();

    parser.parse(new File(xmlfile));
    XSSchemaSet sset = parser.getResult();

    // =========================================================
    // types namepace
    XSSchema gtypesSchema = sset.getSchema("http://www.btec.com/gtypes");
    Iterator<XSComplexType> ctiter = gtypesSchema.iterateComplexTypes();
    while (ctiter.hasNext())
    {
      XSComplexType ct = (XSComplexType) ctiter.next();
      String typeName = ct.getName();
      // these are extensions so look at the base type to see what it is
      String baseTypeName = ct.getBaseType().getName();
      System.out.println(typeName + " is a " + baseTypeName);
    }

    // =========================================================
    // global namespace
    XSSchema globalSchema = sset.getSchema("");
    // local definitions of enums are in complex types
    ctiter = globalSchema.iterateComplexTypes();
    while (ctiter.hasNext())
    {
      XSComplexType ct = (XSComplexType) ctiter.next();
      String typeName = ct.getName();
      String baseTypeName = ct.getBaseType().getName();
      System.out.println(typeName + " is a " + baseTypeName);
    }

    // =========================================================
    // the main entity of this file is in the Elements
    // there should only be one!
    if (globalSchema.getElementDecls().size() != 1)
    {
      throw new Exception("Should be only elment type per file.");
    }

    XSElementDecl ed = globalSchema.getElementDecls().values()
        .toArray(new XSElementDecl[0])[0];
    String entityType = ed.getName();
    XSContentType xsContentType = ed.getType().asComplexType().getContentType();
    XSParticle particle = xsContentType.asParticle();
    if (particle != null)
    {

      XSTerm term = particle.getTerm();
      if (term.isModelGroup())
      {
        XSModelGroup xsModelGroup = term.asModelGroup();
        term.asElementDecl();
        XSParticle[] particles = xsModelGroup.getChildren();
        String propertyName = null;
        String propertyType = null;
        XSParticle pp =particles[0];
        for (XSParticle p : particles)
        {
          XSTerm pterm = p.getTerm();
          if (pterm.isElementDecl())
          {            
            propertyName = pterm.asElementDecl().getName();
            if (pterm.asElementDecl().getType().getName() == null)
            {
              propertyType = pterm.asElementDecl().getType().getBaseType().getName();
            }
            else
            {
              propertyType = pterm.asElementDecl().getType().getName();              
            }
            System.out.println(propertyName + " is a " + propertyType);
          }
        }
      }
    }
    return;
  }
}   

The output from this is:

ENUM is a string
STRING is a string
eSTATUS is a ENUM
eYN is a ENUM
Valid is a eYN
Cal is a STRING
Status is a eSTATUS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文