In Java, how do I parse an xml schema (xsd) to learn what's valid at a given element?

前端 未结 6 1946
感动是毒
感动是毒 2021-02-08 23:01

I\'d like to be able to read in an XML schema (i.e. xsd) and from that know what are valid attributes, child elements, values as I walk through it.

For example, let\'s

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-08 23:14

    Many of the solutions for validating XML in java use the JAXB API. There's an extensive tutorial available here. The basic recipe for doing what you're looking for with JAXB is as follows:

    1. Obtain or create the XML schema to validate against.
    2. Generate Java classes to bind the XML to using xjc, the JAXB compiler.
    3. Write java code to:
      1. Open the XML content as an input stream.
      2. Create a JAXBContext and Unmarshaller
      3. Pass the input stream to the Unmarshaller's unmarshal method.

    The parts of the tutorial you can read for this are:

    1. Hello, world
    2. Unmarshalling XML

提交回复
热议问题