![](/img/trans.png)
[英]XML Marshalling: How to add an attribute from another namespace to an element
[英]Marshalling linking exception when reading XML from namespace
我正在嘗試編組 object 並將其發送到 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
- with linked exception:
[com.sun.istack.internal.SAXException2: com.radial.notification.event.OrderType is not known to this
context
這是 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);
將類設置為綁定解決了問題
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(OrderType.class)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.