XML unmarshalling - java

I want to unmarshall the below XML file
<uc-export clientvers="9.00">
<JOBS_WINDOWS AttrType="WINDOWS" client="0001" name="BEP.WIN.SRZ2AY" system="UC4_EXP">
<ATTR_JOBS state="1">
<AutoDeactNo>6</AutoDeactNo>
<ActAtRun>2</ActAtRun>
</ATTR_JOBS>
</JOBS_WINDOWS>
</uc-export>
want to get the value of in my java program.
I have found a complex solution as of now if there any simple way to proceed rather than creating java class inside another java class.

You can try using JAXB Java Architecture for XML Binding. See here for an example.
Another option to consider is Apache XMLBeans. In XMLBeans, you would create an XSD file which the above XML you gave matches. XMLBeans would compile this XSD file and generate an XmlObject POJO corresponding to the XSD file. Let us call this class (interface) YourXMLDocument. Once these steps are out of the way, you can simply parse your input XML file ("input.xml") with the following line of code:
YourXMLDocument yourDoc = YourXMLDocument.Factory.parse("input.xml");
See here for more information.

Related

How can I parse yaml comments using java?

I want to use a yml configuration file in my project. I am using jackson-dataformat-yaml for parsing yml files. But I need to parse yml comments as well. I used the similar approach in python using ruamel yaml. How can I do the same in java?
Upd.
What for? Well, I wanted to make it possible to override my configuration options by using command line arguments. So, to generate description message for each option, I wanted to use my comments. Like this:
In my config.yml
# Define a source directory
src: '/foo/bar'
# Define a destination directory
dst: '/foo/baz'
So when you run your program with the --help flag, you'll see the following output:
Your program can be ran with the following options:
--src Define a source directory
--dst Define a destination directory
The main benefit in such a model is that you don't ever need to repeat the same statement twice, because they can be retrieved from the configuration file.
Basically, you have three layers of data:
Your configuration schema. This defines the values that are to be defined in the configuration file.
The configuration file itself, which describes the usual configuration on the current machine.
One-time switches, which override the usual configuration.
The descriptions of what each value does belong to the schema, not to the configuration file itself. Think about it: If someone edits the configuration file on their machine and changes the comments, your help output would suddenly show different descriptions.
My suggestion would be to add the descriptions to the schema. The schema is the Java class you load your YAML into. I am not sure why you are using Jackson, since it uses SnakeYaml as parser and SnakeYaml is perfectly able to deserialize into Java classes, but has more configuration options since it does not generalize over JSON and YAML like Jackson does.
Here's a general idea how to do it with SnakeYaml (beware, untested):
// ConfigParam.java
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface ConfigParam { String description(); }
// Configuration.java
public class Configuration {
#ConfigParam("Define a source directory")
String src;
#ConfigParam("Define a destination directory")
String dst;
}
// loading code
Yaml yaml = new Yaml(new Constructor(Configuration.class));
Configuration config = yaml.loadAs(input, Configuration.class);
// help generation code
System.out.println("Your program can be ran with the following options:")
for (Field field: Configuration.class.getFields()) {
ConfigParam ann = field.getAnnotation(ConfigParam.class);
if (ann != null) {
System.out.println(String.format("--%s %s", field.getName(), ann.description());
}
}
For mapping actual parameters to the configuration, you can also loop over class fields and map the parameters to the field names after having loaded the configuration (to replace the standard values with the given ones).

Java: xml to singleton (using maven & jaxb)

I want to do such thing completely automatically by maven.
I have a .xml file with some data. There is some references by ids from one object in this xml to another, and so on. I don't have .xsd of this .xml.
I need exactly two things:
1) Compile this xml to Java classes.
2) Creating one Singleton class named by this .xml name and containing all .xml data relative the inner .xml structure.
For example, my .xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<MyBigSinletonWithAllData>
<Cars>
<Car id="1" speed = "5"/>
<Car id="2" speed = "3"/>
</Cars>
</MyBigSinletonWithAllData>
And I want to get somehting like this, as automatic output:
class MyBigSinletonWithAllData {
List<Cars> cars;
}
class Car {
double speed;
}
class MyBigSinletonWithAllDataSingleton {
MyBigSinletonWithAllData INSTANCE = new MyBigSinletonWithAllData();
/* And here we read an INSTANCE from our .xml file */
}
So, my question is how to do this fully automatically by Maven?
I don't want to code manually the "MyBigSinletonWithAllDataSingleton", I just want to get it already generated for me, so I need only to write code line like this:
MyBigSinletonWithAllDataSingleton.INSTANCE
to get full access to all data that written in relative .xml file.
For now I use "maven-jaxb2-plugin" to generate Java classes from .xsd; But I also need a tool that do .xsd from .xml, and tool that create automatically read/write operations to singleton.
In Java SOAP based webservices there are different implementation like apache axis and apche cxf. In that implementations we have tools which we will run or simply we will pass the XML file to that tool so corresponding binding classes will be generated.
Please look into their code so that it will helps you.
or may be some different tools also will be there(google it).
And yes you can convert xml to xsd file.
Go through the link
Conver XML to XSD

Are there any libraries for extracting class, method, member and field dependency names from a .class file (bytecode)?

As the title says, are there any libraries for extracting class, method, member and field dependency names from a .class file (bytecode)?
For example, if I a compiled Scala .class file uses something like this:
var xs = new List[java.lang.String]();
"blah" :: xs;
xs(0).charAt(0);
I should get that I use these classes and methods:
java.lang.String
java.lang.String#charAt
scala.collection.immutable.List
scala.collection.immutable.List#apply
Is there any library with API I can use in my own Scala program that can do this for me?
Here is ASM framework tutorial explaining how to collect dependencies from classes.
You need to be able to read the code of method bodies. I would use a byte code library such as ObjectWeb's ASM, BCEL or JavaAssist.

WSImports specify package name for a class

I have following scenario, I have two packages say com.ws.a and com.ws.b.
Both the packages having web-services classes. com.ws.a contain class TestServoceA which is having method which returns class TestA.
I want to pass this generated class to TestServiceB which is in com.ws.b.
But wsimport tool generate two different classes for both packages like:
com.ws.a.TestA and
com.ws.b.TestA
I want to generate only one class com.ws.a.TestA as a proxy and use it in both namespaces.
How can I do this? If custom binding can help me please give me example.
Thanks.
wsimport will use JAXB for data binding, follow this documentation to specify custom bindings:
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

XML unmarshalling with Castor and Grails

I have a grails project that contains a few domain objects. I am using a java project in this code which can parse a document for me. The controller that calls that Java project is using JAXB to generate XML from the object returned by the Java project.
I want to use this XML document (which is generated after some text parsing, using JAXB) to populate my Domain classes in my grails project. How does this work in grails? Can I use something like Castor, and create a mapping using the names of my groovy classes? The idea is I want to generate new entries in the database and save it for the user based on whatever text was parsed out of the document they uploaded.
How does this even work in grails anyway? Can I create a new Domain object from another object's controller with something like
Project p = new Project();
and then do a p.save()?
Download the Castor Core and Castor XML jars from here and put them in the lib directory (there's probably a better way to manage this dependency using Grails' dependency management, but this one's a quick and dirty).
With Castor introspection mode you don't need to worry about creating mapping files if your XML matches up nicely with your domains. Here's an example:
grails-app/domain/MyDomain.groovy
class MyDomain {
String foo
String bar
}
grails-app/controllers/MyController.groovy
import org.exolab.castor.xml.Unmarshaller
import java.io.ByteArrayInputStream
class MyController {
def myAction = {
def xml = '''
<myDomain>
<foo>My Foo String</foo>
<bar>My Bar String</bar>
</myDomain>
'''
def reader = new ByteArrayInputStream(xml.bytes).newReader()
def domain = (MyDomain)Unmarshaller.unmarshal(MyDomain.class, reader)
domain.save()
def count = MyDomain.countByFoo('My Foo String')
render "Found $count results"
}
}
Navigate to localhost:8080/appname/my/myAction and it should display "Found N results", N > 0.

Categories

Resources