使用 XSD 将 XML/RDF 转换为 Java 对象

发布于 2024-08-31 14:18:38 字数 1478 浏览 7 评论 0原文

场景如下...我有一个 XSD 文件,描述了我需要的所有对象。我可以使用 JAXB 在 Java 中创建对象,没有问题。我有一个 XML/RDF 文件,需要将其解析为这些对象。

做到这一点最简单的方法是什么?

我一直在研究 Jena 并尝试过它,但不知道如何轻松地将 XML/RDF 文件映射到生成的 XSD 对象。以下是 XSD 文件以及 XML/RDF 文件的片段:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://langdale.com.au/2005/Message#" 
    xmlns:sawsdl="http://www.w3.org/ns/sawsdl" 
    targetNamespace="http://iec.ch/TC57/2007/profile#" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    xmlns="http://langdale.com.au/2005/Message#" 
    xmlns:m="http://iec.ch/TC57/2007/profile#">
<xs:annotation/>
<xs:element name="Profile" type="m:Profile"/>
<xs:complexType name="Profile">
<xs:sequence>
<xs:element name="Breaker" type="m:Breaker" minOccurs="0" maxOccurs="unbounded"/>

以及 XML/RDF:

<!-- CIM XML Output For switch783:(295854688) -->
<cim:Switch rdf:ID="Switch_295854688">
    <cim:IdentifiedObject.mRID>Switch_295854688</cim:IdentifiedObject.mRID>
    <cim:IdentifiedObject.aliasName>Switch_295854688</cim:IdentifiedObject.aliasName>
    <cim:ConductingEquipment.phases 
        rdf:resource="http://iec.ch/TC57/2009/CIM-schema-cim14#PhaseCode.ABC" />
    <cim:Switch.circuit2>0001406</cim:Switch.circuit2>
    <cim:Equipment.Line rdf:resource="#Line_0001406" />

So here's the scenario...I have an XSD file describing all the objects that I need. I can create the objects in Java using JAXB no problem. I have an XML/RDF file that I need to parse into those objects.

What is the EASIEST way to do this?

I have been looking into Jena and have played around with it, but can't see how to easily map the XML/RDF file to the XSD objects that were generated. Here is a snippet of the XSD file as well as the XML/RDF file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://langdale.com.au/2005/Message#" 
    xmlns:sawsdl="http://www.w3.org/ns/sawsdl" 
    targetNamespace="http://iec.ch/TC57/2007/profile#" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified" 
    xmlns="http://langdale.com.au/2005/Message#" 
    xmlns:m="http://iec.ch/TC57/2007/profile#">
<xs:annotation/>
<xs:element name="Profile" type="m:Profile"/>
<xs:complexType name="Profile">
<xs:sequence>
<xs:element name="Breaker" type="m:Breaker" minOccurs="0" maxOccurs="unbounded"/>

And the XML/RDF:

<!-- CIM XML Output For switch783:(295854688) -->
<cim:Switch rdf:ID="Switch_295854688">
    <cim:IdentifiedObject.mRID>Switch_295854688</cim:IdentifiedObject.mRID>
    <cim:IdentifiedObject.aliasName>Switch_295854688</cim:IdentifiedObject.aliasName>
    <cim:ConductingEquipment.phases 
        rdf:resource="http://iec.ch/TC57/2009/CIM-schema-cim14#PhaseCode.ABC" />
    <cim:Switch.circuit2>0001406</cim:Switch.circuit2>
    <cim:Equipment.Line rdf:resource="#Line_0001406" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦萦几度 2024-09-07 14:18:38

您可以迭代 RDF 语句并通过 Bean 填充实用程序(如 BeanUtils)填充您的 JAXB beans。

以这样的形式迭代语句,将具有相同主题的语句分组处理。 rdf:type 语句定义要实例化的类,其余的可能可以映射到创建的 bean 的属性。

如果您熟悉 Java 反射,那么这可能非常简单。

You could iterate through the RDF statements and populate your JAXB beans via a Bean population utility like BeanUtils.

Iterate the statements in such a form that statements with the same subject are processed in a group. The rdf:type statements define which Class to instantiate and the rest can be probably mapped to properties of the created beans.

If you are familiar with Java reflection then this is probably quite straightforward.

鸠书 2024-09-07 14:18:38

您的帖子中不清楚的是 XSD 组件和 RDF 中的特定资源数据(或其架构,例如 RDFS 或 OWL,或两者)之间的任何映射。
如果您了解此映射,那么假设您已经有一个 JAXB 实现来创建 Java 对象(以便用 RDF 表示的数据填充它们) Jena 实现要在 Java 中解析 RDF/XML,那么我建议您可以实现一个 Java“桥”——有效的自定义代码,用于查询 RDF 数据的 Jena 模型,将其映射到 JAXB 生成的类的新对象中,这可以然后将其编组为所需的 XML。

如果您根本不想编写任何 Java 代码来执行此操作,您可以编写一些 XSLT 或 XQuery 来将 RDF/XML 直接转换为所需的 XML,但这听起来会更麻烦。考虑到你已经拥有的东西,比上述选项更有效。

What isn't clear from your post is any mapping between the XSD components and the particular resource data you have in RDF (or schema thereof, such as RDFS or OWL, or both).
If you understand this mapping, then given you have a JAXB implementation to create Java objects already (with a view to populate them with the data represented as RDF) and a Jena implementation to parse the RDF/XML in Java, then I suggest that you can implement a Java 'bridge' - effectively custom code that queries the Jena model of the RDF data to map it into new objects of the classes as generated by JAXB, which can then be marshaled to the required XML.

If you'd rather not write any Java code at all to do this, you could write some XSLT or XQuery to transform your RDF/XML directly into the required XML, but this sounds like it will be more work than the aforementioned option given what you've got already.

茶花眉 2024-09-07 14:18:38

Resource/Subject/etc 对象没有任何转换为​​ DOM 元素的方法吗?
或者(当然不是最好的解决方案)序列化为字符串并读取字符串,然后使用 JAXB 创建的(来自 XSD)对象的 setter 方法怎么样?

Do the Resource/Subject/etc objects not have any methods for converting to DOM Element?
Alternatively (not the neatest solution for sure) what about serializing to string and reading the string then using the JAXB-created (from XSD) objects' setter methods??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文