JAXB编组XMPP节

我正在尝试使用以下代码段整理邮件:


        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和更高版本,所有名称空间都在root元素中声明:


<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>

有没有办法通过JAXB 2.0或更高版本在第二个xml节中封送xmpp节?


长话短说,这里有两个问题:1.在适当的位置声明名称空间。2.删除我知道可以使用NamespacePrefixMapper删除的名称空间前缀吗?虽然不确定,但举个例子会很好。


MM们
浏览 369回答 2
2回答

蓝山帝景

怎么样?创建一个自定义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();MyXMLStreamWriterimport java.util.Iterator;import javax.xml.namespace.NamespaceContext;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamWriter;public class MyXMLStreamWriter implements XMLStreamWriter {&nbsp; &nbsp; private XMLStreamWriter xsw;&nbsp; &nbsp; private MyNamespaceContext nc = new MyNamespaceContext();&nbsp; &nbsp; public MyXMLStreamWriter(XMLStreamWriter xsw) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; this.xsw = xsw;&nbsp; &nbsp; &nbsp; &nbsp; xsw.setNamespaceContext(nc);&nbsp; &nbsp; }&nbsp; &nbsp; public void close() throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.close();&nbsp; &nbsp; }&nbsp; &nbsp; public void flush() throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.flush();&nbsp; &nbsp; }&nbsp; &nbsp; public NamespaceContext getNamespaceContext() {&nbsp; &nbsp; &nbsp; &nbsp; return xsw.getNamespaceContext();&nbsp; &nbsp; }&nbsp; &nbsp; public String getPrefix(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; return xsw.getPrefix(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public Object getProperty(String arg0) throws IllegalArgumentException {&nbsp; &nbsp; &nbsp; &nbsp; return xsw.getProperty(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void setDefaultNamespace(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.setDefaultNamespace(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {&nbsp; &nbsp; }&nbsp; &nbsp; public void setPrefix(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.setPrefix(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeAttribute(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeAttribute(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeAttribute(arg0, arg1, arg2);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeAttribute(arg0, arg1, arg2, arg3);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeCData(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeCData(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeCharacters(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeCharacters(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeCharacters(arg0, arg1, arg2);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeComment(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeComment(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeDTD(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeDTD(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeDefaultNamespace(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeDefaultNamespace(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEmptyElement(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEmptyElement(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEmptyElement(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEmptyElement(arg0, arg1, arg2);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEndDocument() throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEndDocument();&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEndElement() throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEndElement();&nbsp; &nbsp; }&nbsp; &nbsp; public void writeEntityRef(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeEntityRef(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeNamespace(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; }&nbsp; &nbsp; public void writeProcessingInstruction(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeProcessingInstruction(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeProcessingInstruction(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartDocument() throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartDocument();&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartDocument(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartDocument(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartDocument(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartElement(String arg0) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartElement(arg0);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartElement(String arg0, String arg1) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartElement(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException {&nbsp; &nbsp; &nbsp; &nbsp; xsw.writeStartElement("", arg1, arg2);&nbsp; &nbsp; &nbsp; &nbsp; if(null != arg2 || arg2.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String currentDefaultNS = nc.getNamespaceURI("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!arg2.equals(currentDefaultNS)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeDefaultNamespace(arg2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nc.setDefaultNS(arg2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; private static class MyNamespaceContext implements NamespaceContext {&nbsp; &nbsp; &nbsp; &nbsp; private String defaultNS = "";&nbsp; &nbsp; &nbsp; &nbsp; public void setDefaultNS(String ns) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultNS = ns;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getNamespaceURI(String arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if("".equals(arg0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return defaultNS;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getPrefix(String arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Iterator getPrefixes(String arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

烙印99

如今,您还可以使用自定义映射器来控制前缀。&nbsp; &nbsp; NamespacePrefixMapper namespacePrefixMapper = new com.sun.xml.bind.marshaller.NamespacePrefixMapper() {&nbsp; &nbsp; &nbsp; &nbsp; private Map<String, String> prefixes;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefixes = new HashMap<>(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefixes.put(XMLConstants.XML_NS_URI, XMLConstants.XML_NS_PREFIX);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefixes.put(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefixes.put(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefixes.put(WellKnownNamespace.XML_MIME_URI, "xmime");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getPreferredPrefix(String namespaceUri, String suggestion,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean requirePrefix) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String prefix = suggestion == null ? prefixes.get(namespaceUri)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : suggestion;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",&nbsp; &nbsp; &nbsp; &nbsp; namespacePrefixMapper);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java