Step 1: generate the corresponding Java class according to XSD: execute the following command to generate the Java class, X
JC
-D
JavaClass storage path
-PClass package name XSD file name
Example: D: \ Java \ jdk1.6.0 _ 31 \ bin> xjc-d e: \-P org. nssd e: \ journal. XSD
2. Test XML data can be generated based on XSD. test data can be generated using tools such as Eclipse. Right-click the XSD file generate → XML files
3. parse the XML file with the following code:
import java.io.File;import javax.xml.XMLConstants;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBElement;import javax.xml.bind.Unmarshaller;import javax.xml.transform.stream.StreamSource;import javax.xml.validation.SchemaFactory;import org.nssd.JElem;public class Test {public static void main(String[] args) {JAXBContext context;try {context = JAXBContext.newInstance(JElem.class);Unmarshaller shaller = context.createUnmarshaller();shaller.setSchema(SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new File("src/myxsd.xsd")));JAXBElement<JElem> root = shaller.unmarshal(new StreamSource(new File("src/data.xml")),JElem.class);JElem jElem = root.getValue();} catch (Exception e) {e.printStackTrace();}}}
Jelem is the root node of the XML file.