简体   繁体   English

在Matlab中解析xml(使用JAXB)

[英]Parsing xml in Matlab (using JAXB)

I'm trying to parse a xml-file based on a xsd in Matlab. 我正在尝试基于Matlab中的xsd解析xml文件。

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. 如该线程中的建议, MATLAB用XSD解析和存储XML ,我试图使用JAXB来做到这一点,但是我陷入了困境。

I was able to create the .java class files representing my xsd-levels via 我能够通过创建代表我的xsd级别的.java类文件

xjc myFile.xsd –d myDirectory (within the windows command-line). xjc myFile.xsd –d myDirectory (在Windows命令行中)。

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? 在Matlab中继续学习之前,下一步要做什么?

Thanks in advance 提前致谢

Edit: 编辑:

Matlab Version is 2010b (Java 1.6.0_17) Matlab版本为2010b(Java 1.6.0_17)

Edit2: 编辑2:

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). 我现在尝试JAXB.unmarshal(input, MyClass.class)因为我设法让MyClass.class通过使用日食JAXB项目(仅使用上述命令行中输入刚刚给我的MyClass.java文件)。 But Matlab can't find the method JAXB.unmarshal(probably because of the java version of matlab?). 但是Matlab找不到方法JAXB.unmarshal(可能是因为matlab的Java版本?)。
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: 在将我的包所在的文件夹( D:\\PathToMyPackage\\my\\Package\\MyClass.class )添加到Matlab javaaddpath('D:\\PathToMyPackage')的动态Java路径之后,并导入“ my.package”后,我还在尝试:
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. 但是每次我得到一个JAXB Exception例如。

javax.xml.bind.JAXBException: "my.package" doesnt contain ObjectFactory.class or jaxb.index javax.xml.bind.JAXBException:“ my.package”不包含ObjectFactory.class或jaxb.in​​dex

although they're inside the folder. 尽管它们位于文件夹中。

So is it even possible to perform the whole JAXB-thing only in Matlab? 那么,甚至有可能仅在Matlab中执行整个JAXB事情吗? 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? 还是我首先必须在eclipse中包装一些类才能将整个内容导出为.jar文件,然后将其导入到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? eclipse中较新的Java版本,Matlab中的动态Java路径?

If you use Java 8 and don't need xml validation, converting .xml source into Java object is quite easy: 如果您使用Java 8并且不需要xml验证,则将.xml源转换为Java对象非常容易:

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. 在这里, inputInputStreamFileURL等,指向.xml源, MyClass是代表根元素的类-一个具有@XmlRootElement批注的类。 It can be found among classes that were generated by xjc. 可以在xjc生成的类中找到它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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