问题描述
我遇到了这个XSD和JAXB问题...请帮帮我...
I am stuck with this XSD and JAXB problem... Please help me out...
我需要实现的是完全生成下面的xml。
What I need to achieve is to fully generate the xml below.
<?xml version="1.0"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
....
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1">
....
</IRenvelope>
</Body>
</GovTalkMessage>
这是我需要从Java创建的完整xml文件的示例:。
Here's an example of the full xml file I need to create from Java: SUBMISSION_REQUEST.xml .
以下是我所做的总结:
- 生成Java类来自两个涉及的XSD文件。涉及的两个XSD文件是:
envelope-v2-0-HMRC.xsd - 链接到文件:https:// drive.google.com/file/d/0Bwota60eLfeIN1duSGVhTE8xOWM/view?usp=sharing
FullPaymentSubmission-2016-v1-2.xsd - 链接到文件:https:// drive.google.com/file/d/0Bwota60eLfeIbUtMN1RaMmt0LWM/view?usp=sharing - 创建单元测试根据SUBMISSION_REQUEST.xml
- 中的值设置GovTalkMessage对象的值(这是我遇到问题的地方)。设置Body的值,我需要形成IRenvelope来完成xml,但我得到的是一个带有不完整setter的Body类(没有IRenvelope)。
主要的xml信封是GovTalkMessage,用于向英国政府的HMRC发送消息。似乎GovTalkMessage信封已经可以重复使用,它可以通过特别使用Body标签接受不同的消息。
The main xml envelope is GovTalkMessage which is used to send messages to UK Government's HMRC. It appears that the GovTalkMessage envelope has been made reusable that it can accept different messages by particularly using the Body tag.
我的困境是在 Body 标记我需要能够将来自FullPaymentSubmission-2016-v1-2.xsd的 IRenvelope 作为IRenvelope的XML Schema放置,并且能够在Java中设置与此对象关联的值这样我就可以创建整个xml,如SUBMISSION_REQUEST.xml所示
My dilemma is that inside the Body tag I need to be able to place IRenvelope coming from FullPaymentSubmission-2016-v1-2.xsd as the XML Schema for the IRenvelope, and be able to set the values associated to this object in Java so that I could create the whole xml as seen on SUBMISSION_REQUEST.xml
GovTalkMessage的XSD是envelope-v2-0-HMRC.xsd。您将看到匿名Body类型下的内容是 anyAttribute 和 any 。但我需要的是IRenvelope。
The XSD for the GovTalkMessage is envelope-v2-0-HMRC.xsd. You will see that what is under the anonymous Body type is anyAttribute and any. but what I need is IRenvelope.
为了更清楚地解释,我已经为envelope-v2-0-HMRC.xsd生成了Java类,并给了我GovTalkMessage。 java这是我需要设置值的主要等效xml GovTalkMessage信封。
To explain more clearly, I've generated the Java classes for envelope-v2-0-HMRC.xsd and gave me the GovTalkMessage.java which is the main equivalent xml GovTalkMessage envelope I need to set values to.
以下是我使用的xjc命令:
Below are the xjc commands I used:
xjc -p com.rti.rim2016。 v1_2.envelope envelope-v2-0-HMRC.xsd
xjc -p com.rti.rim2016.v1_2.fps FullPaymentSubmission-2016- v1-2.xsd
主要问题是使用 ## any的 GovTalkMessage Body元素和 xsd:anyAttribute
The main problem is with the GovTalkMessage "Body" element that uses ##any and xsd:anyAttribute
<xsd:element minOccurs="0" maxOccurs="1" name="Body">
<xsd:complexType>
<xsd:sequence>
<xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute namespace="##any" processContents="strict"/>
</xsd:complexType>
</xsd:element>
为 envelope-v2-0-HMRC.xsd生成相应的Java类并获得了GovTalkMessage.java的代码(我只提供了与body元素相关的代码段)。
Generated the corresponding Java classes for envelope-v2-0-HMRC.xsd and got the code for GovTalkMessage.java (I only provided the snippet related to the "body" element).
显然没有setter方法可以重定向设置值与IRenvelope相关,以便我可以完成XML的构建。
Obviously there was no setter method to redirectly set the values related to IRenvelope so that I could complete the build of the XML.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"envelopeVersion",
"header",
"govTalkDetails",
"body"
})
@XmlRootElement(name = "GovTalkMessage", namespace = "http://www.govtalk.gov.uk/CM/envelope")
public class GovTalkMessage {
@XmlElement(name = "Body", namespace = "http://www.govtalk.gov.uk/CM/envelope")
protected GovTalkMessage.Body body;
....
public GovTalkMessage.Body getBody() {
return body;
}
/**
* Sets the value of the body property.
*
* @param value
* allowed object is
* {@link GovTalkMessage.Body }
*
*/
public void setBody(GovTalkMessage.Body value) {
this.body = value;
}
我只看到我玩过的getAny和getOtherAttributes并没有工作。
I only see the getAny and getOtherAttributes which I have played around and didn't work.
//Body
Body body = new Body();
IRenvelope irEnvelope = new IRenvelope();
body.getAny().add(irEnvelope);
govTalkMessage.setBody(body);
我尝试使用从FullPaymentSubmission-2016-v1-2生成的IRenvelope java类实例设置getAny。 xsd但得到以下错误:
I tried setting getAny with an instance of IRenvelope java class generated from FullPaymentSubmission-2016-v1-2.xsd but got the error below:
错误:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: class com.rti.rim2016.v1_2.fps.generated.IRenvelope nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class com.upraxis.rti.rim2016.v1_2.fps.generated.IRenvelope nor any of its super class is known to this context.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116)
我希望有人能帮助我阐明如何正确解决这个问题。
I hope someone could help me shed some light on how to correctly solve this problem.
推荐答案
答案
你应该只需要创建你的 JAXBContext
在冒号分隔的字符串
上,包含两个生成的包:
Answer
You should just need to create your JAXBContext
on a colon delimited String
with the two generated packages:
JAXBContext jc = JAXBContext.newInstance("com.rti.rim2016.v1_2.fps:com.rti.rim2016.v1_2.envelope");
演示代码
然后当我跑:
Demo Code
Then when I run:
import javax.xml.bind.*;
import com.rti.rim2016.v1_2.fps.*;
import com.rti.rim2016.v1_2.envelope.GovTalkMessage;
import com.rti.rim2016.v1_2.envelope.GovTalkMessage.Body;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("com.rti.rim2016.v1_2.fps:com.rti.rim2016.v1_2.envelope");
GovTalkMessage govTalkMessage = new GovTalkMessage();
Body body = new Body();
IRenvelope irEnvelope = new IRenvelope();
body.getAny().add(irEnvelope);
govTalkMessage.setBody(body);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(govTalkMessage, System.out);
}
}
我得到以下输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:GovTalkMessage xmlns="http://www.govtalk.gov.uk/taxation/PAYE/RTI/FullPaymentSubmission/15-16/1" xmlns:ns2="http://www.govtalk.gov.uk/CM/envelope" xmlns:ns3="http://www.w3.org/2000/09/xmldsig#">
<ns2:Body>
<IRenvelope/>
</ns2:Body>
</ns2:GovTalkMessage>
这篇关于使用元素“anyAttribute”将XML编组到Java和“任何”在XSD中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!