Friday, April 6, 2018

Xml Parsing, tools, strategies and alternatives

JAXB



If you have an xsd, you can generate the @javax.xml.bind.annotation.XmlRootElement annotated Java POJOs with xjc:
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html
example:
cd D:\pierre\wildfly-11.0.0.CR1\docs\schema

xjc .\wildfly-config_5_0.xsd -d D:\pierre\workspaceneon\PVXmlParser\src\


Here an example to PARSE an xml file https://www.javacodegeeks.com/2013/02/jaxb-tutorial-getting-started.html

Here the https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/Unmarshaller.html

To generate an XSD from existing XML, try http://www.thaiopensource.com/relaxng/trang-manual.html (a bit old tool...)

package org.pierre.jbossconf;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import jboss.domain._5.Server;


public class XmlParser {
 public static void main(String[] args) throws JAXBException {
  JAXBContext jaxbContext = JAXBContext.newInstance(Server.class);
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  File standaloneXmlFile = new File("D:\\pierre\\wildfly-11.0.0.CR1\\standalone\\configuration\\standalone.xml");
  Server server = (Server)jaxbUnmarshaller.unmarshal(standaloneXmlFile);
  server.getProfile().getAny().forEach((item) -> {System.out.println(item);}); 
 }

}



in my case, parsing fails because of missing tokes substitution "Not a number: ${jboss.socket.binding.port-offset:0}"

these variables are present in thee standalone.xml file:
jboss.deployment.scanner.rollback.on.failure
prop.remoting-connector.read.timeout
jboss.home.dir
jboss.bind.address
jboss.bind.address.management
jboss.bind.address
jboss.socket.binding.port-offset
jboss.management.http.port
jboss.management.https.port
jboss.ajp.port
jboss.http.port
jboss.https.port


No comments: