简体   繁体   中英

How to parse Generic XSD documents

I need to pase some XSD documents.

I am not asking about the API set, I found this very helpful API

the issues here is..

How can I know which element is the parent of which element... In other words.. How can I exectract the hirarecy of the document elements (it is very simple in XML)

The XSD seems different..

Here's an example:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Journal">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="JournalName"/>
        <xs:element ref="Volumes"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Journals">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="Journal"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
.....

As appears above, although Journal is sub element of Journals , but It appears above it. (that couldn't happen in the nested-manner of XML docuemtns)

Thanks.

It seems your thinking is a little confused. Why would the definition of Journals have to occur before that of Journal? Those definitions don't contain one another - they define elements which contain one another, but the definitions are separate. Consider some corresponding Java code:

class Journal {
    String journalName;
    List<Volume> volumes;
}

class Journals {
    List<Journal> journals;
}

You wouldn't expect to see those in any particular order, let alone for the definition of Journal to occur inside that for Journals, would you?

So, if you want to extract the hierarchy from the schema, i'm afraid you'll have to read through the whole document, and note where an element is defined as containing some other element. You can build up a description of the hierarchy as you go.

Try to read up on XSD (for example: w3schools ). The above XSD mentions that an XML that conforms to this XSD can have an element Journals which contains several subelements Journal. It may appear as though Journals is a subelement but it's not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM