Java JAXB编组XMPP节
我正试图使用以下代码片段整理一条消息:Java JAXB编组XMPP节,java,jaxb,xmpp,marshalling,Java,Jaxb,Xmpp,Marshalling,我正试图使用以下代码片段整理一条消息: JAXBContext jContext = JAXBContext.newInstance(Iq.class); Marshaller m = newJAXBContext.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_FORMA
JAXBContext jContext = JAXBContext.newInstance(Iq.class);
Marshaller m = newJAXBContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Bind bind = new Bind();
bind.setResource("resource");
Iq iq = new Iq();
iq.setId(iqId);
iq.setType("set");
iq.getAnies().add(bind);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.marshal(iq, baos);
这里,Iq和Bind是由相关xmpp模式形成的对象。我的问题是,对于jaxb 2.0和更高版本,所有名称空间都在根元素中声明:
<iq from='juliet@example.com/balcony'
id='rg1'
type='get' xmlns='jabber:client' xmlns:ns1='urn:ietf:params:xml:ns:xmpp-bind'>
<ns1:bind>
<ns1:resource>resource</ns1:resource>
</ns1:bind>
</iq>
资源
但是,这里需要的是名称空间应该占据适当的位置:
<iq from='juliet@example.com/balcony'
id="rg1"
type="get" xmlns="jabber:client">
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
<resource>resource</resource>
</bind>
</iq>
资源
有没有一种方法可以通过JAXB2.0或更高版本在第二节xml中看到的xmpp节进行整理
长话短说,我这里有两个问题:
1.在适当的位置声明名称空间。
2.删除我理解的名称空间前缀可以使用NamespacePrefixMapper删除?不确定,举个例子就好了。下面的例子怎么样 创建一个自定义XMLStreamWriter,将所有名称空间声明视为默认名称空间,然后封送到该名称空间:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
xsw = new MyXMLStreamWriter(xsw);
m.marshal(iq, xsw);
xsw.close();
MyXMLStreamWriter
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class MyXMLStreamWriter implements XMLStreamWriter {
private XMLStreamWriter xsw;
private MyNamespaceContext nc = new MyNamespaceContext();
public MyXMLStreamWriter(XMLStreamWriter xsw) throws Exception {
this.xsw = xsw;
xsw.setNamespaceContext(nc);
}
public void close() throws XMLStreamException {
xsw.close();
}
public void flush() throws XMLStreamException {
xsw.flush();
}
public NamespaceContext getNamespaceContext() {
return xsw.getNamespaceContext();
}
public String getPrefix(String arg0) throws XMLStreamException {
return xsw.getPrefix(arg0);
}
public Object getProperty(String arg0) throws IllegalArgumentException {
return xsw.getProperty(arg0);
}
public void setDefaultNamespace(String arg0) throws XMLStreamException {
xsw.setDefaultNamespace(arg0);
}
public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {
}
public void setPrefix(String arg0, String arg1) throws XMLStreamException {
xsw.setPrefix(arg0, arg1);
}
public void writeAttribute(String arg0, String arg1) throws XMLStreamException {
xsw.writeAttribute(arg0, arg1);
}
public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException {
xsw.writeAttribute(arg0, arg1, arg2);
}
public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {
xsw.writeAttribute(arg0, arg1, arg2, arg3);
}
public void writeCData(String arg0) throws XMLStreamException {
xsw.writeCData(arg0);
}
public void writeCharacters(String arg0) throws XMLStreamException {
xsw.writeCharacters(arg0);
}
public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException {
xsw.writeCharacters(arg0, arg1, arg2);
}
public void writeComment(String arg0) throws XMLStreamException {
xsw.writeComment(arg0);
}
public void writeDTD(String arg0) throws XMLStreamException {
xsw.writeDTD(arg0);
}
public void writeDefaultNamespace(String arg0) throws XMLStreamException {
xsw.writeDefaultNamespace(arg0);
}
public void writeEmptyElement(String arg0) throws XMLStreamException {
xsw.writeEmptyElement(arg0);
}
public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {
xsw.writeEmptyElement(arg0, arg1);
}
public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException {
xsw.writeEmptyElement(arg0, arg1, arg2);
}
public void writeEndDocument() throws XMLStreamException {
xsw.writeEndDocument();
}
public void writeEndElement() throws XMLStreamException {
xsw.writeEndElement();
}
public void writeEntityRef(String arg0) throws XMLStreamException {
xsw.writeEntityRef(arg0);
}
public void writeNamespace(String arg0, String arg1) throws XMLStreamException {
}
public void writeProcessingInstruction(String arg0) throws XMLStreamException {
xsw.writeProcessingInstruction(arg0);
}
public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {
xsw.writeProcessingInstruction(arg0, arg1);
}
public void writeStartDocument() throws XMLStreamException {
xsw.writeStartDocument();
}
public void writeStartDocument(String arg0) throws XMLStreamException {
xsw.writeStartDocument(arg0);
}
public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {
xsw.writeStartDocument(arg0, arg1);
}
public void writeStartElement(String arg0) throws XMLStreamException {
xsw.writeStartElement(arg0);
}
public void writeStartElement(String arg0, String arg1) throws XMLStreamException {
xsw.writeStartElement(arg0, arg1);
}
public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException {
xsw.writeStartElement("", arg1, arg2);
if(null != arg2 || arg2.length() > 0) {
String currentDefaultNS = nc.getNamespaceURI("");
if(!arg2.equals(currentDefaultNS)) {
writeDefaultNamespace(arg2);
nc.setDefaultNS(arg2);
}
}
}
private static class MyNamespaceContext implements NamespaceContext {
private String defaultNS = "";
public void setDefaultNS(String ns) {
defaultNS = ns;
}
public String getNamespaceURI(String arg0) {
if("".equals(arg0)) {
return defaultNS;
}
return null;
}
public String getPrefix(String arg0) {
return "";
}
public Iterator getPrefixes(String arg0) {
return null;
}
}
}
现在还可以使用自定义映射器控制前缀
NamespacePrefixMapper namespacePrefixMapper = new com.sun.xml.bind.marshaller.NamespacePrefixMapper() {
private Map<String, String> prefixes;
{
prefixes = new HashMap<>(3);
prefixes.put(XMLConstants.XML_NS_URI, XMLConstants.XML_NS_PREFIX);
prefixes.put(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi");
prefixes.put(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs");
prefixes.put(WellKnownNamespace.XML_MIME_URI, "xmime");
}
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion,
boolean requirePrefix) {
String prefix = suggestion == null ? prefixes.get(namespaceUri)
: suggestion;
return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
}
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
namespacePrefixMapper);
NamespacePrefixMapper NamespacePrefixMapper=new com.sun.xml.bind.marshaller.NamespacePrefixMapper(){
私有地图前缀;
{
前缀=新的HashMap(3);
prefixes.put(xmlstants.XML_NS_URI,xmlstants.XML_NS_PREFIX);
prefixes.put(xmlstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,“xsi”);
prefixes.put(xmlstants.W3C_XML_SCHEMA_NS_URI,“xs”);
prefixes.put(WellKnownNamespace.XML_MIME_URI,“xmime”);
}
@凌驾
公共字符串getPreferredPrefix(字符串名称空间URI、字符串建议、,
布尔值(REPREFIX){
字符串前缀=suggestion==null?前缀.get(namespaceUri)
:建议;
返回前缀==null?XMLConstants.DEFAULT\u NS\u前缀:前缀;
}
};
marshaller.setProperty(“com.sun.xml.bind.namespacePrefixMapper”,
namespacePrefixMapper);
假设在使用名称空间之前声明名称空间,那么在何处声明名称空间并不重要。没有“适当的位置”,但是如果名称空间被压缩在根元素中,我正在使用的XMPP服务器似乎不会响应消息。我相信它希望将名称空间放在单个标记上。Blaise,你知道XMLOutputFactory和XMLStreamWriter是否是线程安全的吗?我试图在网上找到答案,但找不到有答案的可靠资源。