英文:
Marshalling linking exception when reading XML from namespace
问题
我正在尝试编组对象并将其发送到JMS。
我可能犯了什么错误?
QName qName = new QName("http://schema.gspt.net/EventCanonical/1.0", "OrderType");
JAXBElement<OrderType> jaxbElement = new JAXBElement(qName, OrderType.class, orderType);
jaxb2Marshaller.createMarshaller().marshal(jaxbElement, sw);
javax.xml.bind.MarshalException
- 与链接的异常:
[com.sun.istack.internal.SAXException2:com.radial.notification.event.OrderType在此上下文中未知]
这是XML中的命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://schema.gspt.net/EventCanonical/1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schema.gspt.net/EventCanonical/1.0" elementFormDefault="qualified"
attributeFormDefault="unqualified">
以前的传统方法实际上是有效的:
JAXBContext jaxbContext = JAXBContext.newInstance(OrderType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(orderType, sw);
英文:
I am trying to marshall the object and send it to JMS.
What could be the mistake I am doing?
QName qName = new QName("http://schema.gspt.net/EventCanonical/1.0","OrderType");
JAXBElement<OrderType> jaxbElement = new JAXBElement( qName, OrderType.class,orderType);
jaxb2Marshaller.createMarshaller().marshal(jaxbElement,sw);
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: com.radial.notification.event.OrderType is not known to this
context
This is the namespace in XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://schema.gspt.net/EventCanonical/1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schema.gspt.net/EventCanonical/1.0" elementFormDefault="qualified"
attributeFormDefault="unqualified">
The classic way of doing it actually working
JAXBContext jaxbContext = JAXBContext.newInstance(OrderType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(orderType, sw);
答案1
得分: 1
设置绑定的类解决了问题
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(OrderType.class)
英文:
Setting the classes to bound solved the problem
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(OrderType.class)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论