[英]Create instance of XML document using XSD in Java
I am looking for a Java library that can take an XSD schema and create a sample XML document that can then be manipulated. 我正在寻找一个Java库,该库可以采用XSD架构并创建一个示例XML文档,然后可以对其进行操作。 In my simple example i am given an XML that i load, manipulate and print. 在我的简单示例中,为我提供了一个XML,它可以加载,操作和打印。 How can i achieve the same result if i am only given an XSD? 如果仅获得XSD,如何获得相同的结果?
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ns0:store_msg xmlns:ns0=\"http://data\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://data sample.xsd \">" +
"<item>" +
"<name>A</name>" +
"<price>1.1</price>" +
"</item>" +
"<item>" +
"<name>B</name>" +
"<price>2.2</price>" +
"</item>" +
"</ns0:store_msg>";
// load XML
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
// manipulate XML
Node root = doc.getFirstChild();
Element el = doc.createElement("item");
Element name = doc.createElement("name");
Element price = doc.createElement("price");
name.setTextContent("C");
price.setTextContent("2.2");
el.appendChild(name);
el.appendChild(price);
root.appendChild(el);
// print XML
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
A. use JAXB (preferable): A.使用JAXB(首选):
pre-step: generate JAXB Java classes out of your XSD, as example by maven-jaxb2-plugin or with other tools or manually calling XJC compiler 预先步骤:例如,通过maven-jaxb2-plugin或与其他工具或手动调用XJC编译器,从XSD生成JAXB Java类
when you have needed classes create your root element: 需要类时,请创建您的根元素:
StoreMsg myStoreMsgRoot = new StoreMsg(); // Class must be what JAXB generated for store_msg element
populate needed elements in it as you wish. 根据需要填充其中的所需元素。 as example: 例如:
Item firstItem = new Item(); firstItem.setName("A"); firstItem.setPrice(1.1); myStoreMsgRoot.getItem().add(firstItem);
and so on 等等
Then marshal your root element with JAXBContext as example to String 然后使用JAXBContext将您的根元素编组为String
JAXBContext jaxbCtx = JAXBContext.newInstance(StoreMsg.class);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); jaxbCtx.createMarshaller().marshal(myStoreMsgRoot, bos); String generatedXml = new String(bos.toByteArray()); bos.close();
B. there is a framework to do it - jlibs . B.有一个框架可以做到这一点-jlibs 。
Simple example is here: jlibs - XSInstance.wiki 一个简单的例子在这里: jlibs-XSInstance.wiki
Aside of that you may need to set bunch of properties to deal with cases @VCR commented about. 除此之外,您可能需要设置一些属性来处理@VCR评论的情况。
From first glance it works somehow. 乍一看,它以某种方式起作用。
Just latest versions need dependencies (through Maven as below or jar files in the classpath): 只是最新版本需要依赖项(通过下面的Maven或类路径中的jar文件):
<!-- https://mvnrepository.com/artifact/in.jlibs/jlibs-xsd -->
<dependency>
<groupId>in.jlibs</groupId>
<artifactId>jlibs-xsd</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/in.jlibs/jlibs-xml -->
<dependency>
<groupId>in.jlibs</groupId>
<artifactId>jlibs-xml</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0-22</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.