Parsing xsd file in java




















You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience. Necessary Necessary. Necessary cookies are absolutely essential for the website to function properly.

This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. Non-necessary Non-necessary. In Java, how do I parse an xml schema xsd to learn what's valid at a given element? Ask Question. Asked 10 years, 1 month ago. Active 5 years, 8 months ago. Viewed 17k times. Improve this question. Paul Paul Do you want tools to help explore the xsd or want to process things programmatically?

I'd like to read in an xsd and be able to present the user me, for now anyway valid options for a given point in a validated with the xsd xml document. See stackoverflow. I think that what you want is the same with what someone needs when writing a tool to generate sample XML, or data entry UI, from an XML Schema; maybe this kind of parallel could help steer the answers?

Add a comment. Active Oldest Votes. Let's see how querying context-specific elements can be done by the means of Apache XmlSchema. Improve this answer. Hello, Thanks a lot for this answer.

After looking into this answer I am trying to implement the XSD parser using the XmlSchema Core but I am not sure how can I get all the child elements and their type and other related information.

If possible can you please explain how to get all the child elements for complexity and their type and other associated information? Thanks in advance. I am trying to resolve this for nearly 1 day. I have posted my question here: stackoverflow. Thanks a lot in advance.

Write java code to: Open the XML content as an input stream. Paul Morie Paul Morie I'm sorry, I don't think I explained myself very well. I've added a clarification to my original question. I tinkered with Jaxe a bit but I really need this bit of understanding, not a standalone tool. This answer was closest to what I ended up doing.

Rather than using xsj, though, I found Apache XMLBeans to be much more powerful, and it's pretty easy to use thanks to the sample code. Glad this could help you. I see you have tried Eclipse XSD. Community Bot 1 1 1 silver badge. Thanks for the excellent direction. I've got some reading to do, clearly. File; import java. Iterator; import java. Vector; import org. ErrorHandler; import com. XSComplexType; import com. XSElementDecl; import com. XSFacet; import com. XSModelGroup; import com. XSModelGroupDecl; import com.

XSParticle; import com. Git stats commits. Failed to load latest commit information. View code. All these classes derive from the same abstract class, XsdAbstractElement. For example, the xsd:annotation tag only allows xsd:appinfo and xsd:documentation as children nodes, and also can have an attribute named id , therefore XsdParser has the following class simplified for example purposes :.

First, in order to include it to your Maven project, simply add this dependency:. After parsing the file like shown above it's possible to start to navigate in the resulting parsed elements.

In the image below it is presented the class diagram that could be useful before trying to start navigating in the result. There are multiple abstract classes that allow to implement shared features and reduce duplicated code. Below a simple example is presented.

After parsing the XSD snippet the parsed elements can be accessed with the respective java code. The result could be consulted in the following way:. In order to minimize the number of passages in the file, which take more time to perform, this library chose to parse all the elements and then resolve the references present. Our parse process is also based on a tree approach, which means that when we invoke the XsdSchema parse function the whole document will be parsed, because each XsdAbstractElement class extracts its respective information, i.

This library was born with an objective in mind, it should strictly follow the XSD language rules. To guarantee that we used the Visitor pattern. We used this pattern to add a layer of control regarding different XSD types interactions. In the presented code snippet we can observe how this works:. In this example we can see that XsdComplexContentVisitor class only implements two methods, visit XsdRestriction element and visit XsdExtension element.

This means that the XsdComplexContentVisitor type only allows these two types, i. The XSD syntax also especifies some other restrictions, namely regarding attribute possible values or types. With this we can verify if the received value is a possible value for that respective attribute.

There are other validations, such as veryfing if a given attribute is a positiveInteger, a nonNegativeInteger, etc. If any of these validations fail an exception will be thrown with a message detailing the failed validation. Apart from the type validations the XSD syntax specifies some other rules. These rules are associated with a given XSD type and therefore are verified when an instance of that respective object is parsed.

A simple example of such rule is the following rule: "A xsd:element cannot have a ref attribute if its parent is a xsd:schema element. If the rule is violated then an exception is thrown with a message detailing the issue.

This is a big feature of this library. This generates two problems when handling the parsing which are detailed below. Either the referred element is missing or the element is present and an exchange should be performed. To help in this process we create a new layer with four classes: UnsolvedElement - Wrapper class to each element that has a ref attribute. ConcreteElement - Wrapper class to each element that is present in the file. NamedConcreteElement - Wrapper class to each element that is present in the file and has a name attribute present.

These classes simplify the reference solving process by serving as a classifier to the element that they wrap.



0コメント

  • 1000 / 1000