I am looking for a Java library that can take an XSD schema and create a sample XML document that can then be manipulated. In my simple example i am given an XML that i load, manipulate and print. How can i achieve the same result if i am only given an 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")));
minOccurs="0"? What would a sample XML document contain for an element whose type is defined with<choice>?