Is there any Java API for generating xml at runtime using an input xml schema and a pair of xpath expression values ​​and data

I have a runtime xml schema file that is mutable. I get a java Collection in my code with XPath expressions and a value that needs to be entered into an XML file. Using all of these runtime inputs, I have to generate an XML file. Below are the input and output samples.

  • The following is an example schema (without a fixed format):

    <xs:complexType name="root"> <xs:sequence> <xs:element name="top" type="topType" /> </xs:sequence> </xs:complexType> <xs:element name="root" type="root"> </xs:element> <xs:complexType name="topType"> <xs:element name="mode" use="required" /> <xs:element name="address" use="required" /> </xs:complexType> 
  • The xpath expression and the value i m are obtained in a pair of key values ​​with a Hash Map. I need these xpath values ​​to fit with the corresponding XMLElement value in output.xml. The expressions and xpath values ​​are as follows:

    • Expression: / root / top / address Value: 10.200.111.
    • Expression: / root / top / mode Value: cluster
  • The java code should generate an XML file as Output.xml: (This is a dummy file that needs to be created at runtime from all collected inputs)

     <root> <top> <mode>cluster</mode> <address>10.200.111.111</address> </top> </root> 

Please suggest that someone come across such a scenario.

Thanks in advance.

+4
source share
2 answers

Please check the code below to generate xml from xsd after creating XMLDocument, XPath to update / add fields

 import javax.xml.transform.stream.StreamResult; import jlibs.xml.sax.XMLDocument; import jlibs.xml.xsd.XSInstance; import jlibs.xml.xsd.XSParser; import org.apache.xerces.xs.XSModel; import javax.xml.namespace.QName; public class XSDToXML { /** * @param args * @throws ClassCastException * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException { try { XSModel model = (XSModel) new XSParser().parse("c:\\kar\\xs.xsd"); XMLDocument sample = new XMLDocument(new StreamResult( "c:\\kar\\root3.xml"), false, 4, null); QName root = new QName("root"); XSInstance instance = new XSInstance(); instance.minimumElementsGenerated = 0; instance.maximumElementsGenerated = 0; instance.generateDefaultAttributes = true; instance.generateOptionalAttributes = true; instance.maximumRecursionDepth = 0; instance.generateOptionalElements = true; instance.generate(model, root, sample); } catch (Exception ex) { ex.printStackTrace(); } } } 

U need to load jlibs-xml.jar, xercesImpl.jar and jlibs-core.jar files.

+1
source

Just check this code is pretty simple.

 public class MyTestClass { public static void main(String[] args) { try{ Map map= new HashMap<String,String>(); map.put("cluster", "10.200.111.111"); map.put("cluster1", "10.200.121.111"); MyXML xml = new MyXML(); List<Top> top1= new ArrayList<Top>(); Set<String> keys = map.keySet(); for(String key : keys){ Top top=new Top(); top.setMode(key); top.setAddress((String)map.get(key)); top1.add(top); } xml.setTop(top1); File file = new File("C:\\kar\\file.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(MyXML.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(xml, file); jaxbMarshaller.marshal(xml, System.out); }catch(Exception ex){ System.out.println(ex.getMessage()); } } } @XmlRootElement(name="top") @XmlType(name="top") @XmlAccessorType(XmlAccessType.FIELD) public class Top { @XmlElement(name="mode", required=true) private String mode; @XmlElement(name="mode", required=true) private String address; public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } @XmlRootElement(name="root") @XmlType(name="root") @XmlAccessorType(XmlAccessType.FIELD) public class MyXML { @XmlElement(name="Top") private List<Top> top; public List<Top> getTop() { return top; } public void setTop(List<Top> top) { this.top = top; } } 

You can even do this with XSLT, http://www.ictforu.com/index.php/Core-Java/java-xslt.html

0
source

Source: https://habr.com/ru/post/1481095/


All Articles