0

I'm trying to parse a xml-file based on a xsd in Matlab.

As recommended in this thread MATLAB parse and store XML with XSD, I'm trying to use JAXB to do this, but I got stuck.

I was able to create the .java class files representing my xsd-levels via

xjc myFile.xsd –d myDirectory (within the windows command-line).

I've read the linked articles in the other thread, but now I don't know how to go on.

What are the next steps before I can go on in Matlab?

Thanks in advance

Edit:

Matlab Version is 2010b (Java 1.6.0_17)

Edit2:

I tried now JAXB.unmarshal(input, MyClass.class) as I managed to get the MyClass.class by using an eclipse JAXB-Project (only using the command line input mentioned above just gave me the MyClass.java files). But Matlab can't find the method JAXB.unmarshal(probably because of the java version of matlab?).
After adding the folder, where my package is in(D:\PathToMyPackage\my\Package\MyClass.class), to the dynamic java path in Matlab javaaddpath('D:\PathToMyPackage') and after importing 'my.package' I was also trying:
jc = JAXBContext.newInstance('my.package')
jc = JAXBContext.newInstance('ObjectFactory.class')
jc = JAXBContext.newInstance('my.package.ObjectFactory.class')

But each time I get a JAXB Exception eg.

javax.xml.bind.JAXBException: "my.package" doesnt contain ObjectFactory.class or jaxb.index

although they're inside the folder.

So is it even possible to perform the whole JAXB-thing only in Matlab? Or do I first have to wrap around some classes in eclipse to export the whole thing as a .jar-file, which I then import in Matlab?

If it is possible, which mistakes could I make? Or what could have an impact? Newer Java version in eclipse, the dynamic java path in Matlab?

Community
  • 1
  • 1
m8sch
  • 13
  • 3
  • Where are you stuck ? The linked question asks you to import the generated java files in Matlab... – Sid Aug 26 '15 at 15:38

1 Answers1

0

If you use Java 8 and don't need xml validation, converting .xml source into Java object is quite easy:

MyClass myObject = JAXB.unmarshal(input, MyClass.class);

Here, input is either InputStream, File, URL etc, pointing to .xml source, MyClass is a class that represents root element--one which has @XmlRootElement annotation. It can be found among classes that were generated by xjc.

See more: https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXB.html

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • thanks for the answer, but unfortunately I use Java 1.6_17 in Matlab 2010b. I tried a few things but couldn't get it working (see my Edit above) – m8sch Aug 27 '15 at 14:32