简体   繁体   中英

Parsing xml in Matlab (using JAXB)

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM