Operations on the XSD file generate a Java object and parse the generated XML

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.