首页 文章

JAXB marshlling不会忽略命名空间

提问于
浏览
0

我花了一些时间来调查问题是什么,但我无法解决 . 当我在XML下解组并编组时,我看到了不同的XML .

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
 <one>test</one>
 <three>\MySG\test.jsp</three>
 <two>
    <st>
        <seeta>
            <Source>

                <problemtag xmlns="uuid:B89290D2-36FB-4EBC-A581-69B16D59EB92">
                    <p>deploy_test_page_renderingMetadata</p>
                </problemtag>
            </Source>
        </seeta>
        <Template id="tcm:1-63-32" title="Smart Compound Component Template"/>
        <Publication id="tcm:0-1-1" title="Publication"/>
    </st>
 </two>
</root>
  • 在上面的xml中,只有一个标记(第一个)预期剩余的所有(包括命名空间)都是意外的元素 . 另一个应用程序发送上述XML .

我的映射是这样的

package com.seeta.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    @XmlElement(name="one")
    private String one;

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    @XmlElement(name="three")
    private String three;

    @XmlAnyElement
    private List<Object> remaining = new ArrayList<Object>();


    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }

    public List<Object> getRemaining() {
        return remaining;
    }

    public void setRemaining(List<Object> remaining) {
        this.remaining = remaining;
    }

    public String toString() {
        return String.format("One [%s]-> Number of remaing elements [%d]-> three [%s]", one, remaining.size(), three);
    }
}

这是我的简单代码

package com.seeta.xml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLDecoder;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
        public class JaxbSample {

        public Document getDOMDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            if (inputStream != null) {
                return documentBuilder.parse(new InputSource(inputStream));
            } else {
                return documentBuilder.newDocument();
            }
        }
        public Root unmarshall(Document document) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(Root.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Root root = (Root) unmarshaller.unmarshal(document);
            return root;
        }

        public Document marshall(Root root) throws JAXBException, ParserConfigurationException, SAXException, IOException {
            JAXBContext context = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = context.createMarshaller();
            Document document = getDOMDocument(null);
            marshaller.marshal(root, document);
            return document;
        }

        private String transform(Document document) throws TransformerException {
            StringWriter sw = new StringWriter();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        }

        public void testUnmarshallMarshallUsingDocument() throws ParserConfigurationException, SAXException, IOException, JAXBException, TransformerException {
            InputStream inputStream = this.getClass().getResourceAsStream("jaxb.xml");
            Document document = getDOMDocument(inputStream);
            Root root = unmarshall(document);
            Document documentAfterMarshal = marshall(root);
            String output = transform(documentAfterMarshal);
            System.out.println(output);
        }

        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, JAXBException, TransformerException {
            JaxbSample jaxbTest = new JaxbSample();
            jaxbTest.testUnmarshallMarshallUsingDocument();
        }
    }

输出是

<root>
<one>test</one>
<three>\MySG\test.jsp</three>
<two>
<st>
<seeta>
<Source>
<problemtag:problemtag xmlns="uuid:B89290D2-36FB-4EBC-A581-69B16D59EB92" xmlns:problemtag="uuid:B89290D2-36FB-4EBC-A581-69B16D59EB92">
<p>deploy_test_page_renderingMetadata</p>
                </problemtag:problemtag>
            </Source>
        </seeta>
<Template id="tcm:1-63-32" title="Smart Compound Component Template"/>
<Publication id="tcm:0-1-1" title="Publication"/>
    </st>
 </two>
</root>

我还试着跟随

  • 我尝试使用NamespacePrefixMapper . 我可以给出不同的命名空间但不能为空(“”) . 我根本不需要任何名称空间 .

new NamespacePrefixMapper(){public String getPreferredPrefix(String namespaceUri,String suggestion,boolean requirePrefix){return“”; }};

  • 我们的项目中没有任何xsd(至少我不知道)尝试不合格的东西

  • 我真的不明白QName的事情

2 回答

  • 0

    如果您只想保留未使用的元素并将其编组回来,我认为您应该能够做到这样的事情:

    @XmlRootElement(name="Root")
    @XmlAccessorType(XmlAccessType.FIELD)
    class Root {
        @XmlElement(name="One")
        private String one;
    
        @XmlAnyElement
        private List<Any> otherElements;
    }
    
    class AnyAdapter extends XmlAdapter<Element,Any> {
        @Override
        public Any unmarshal(Element element) throws Exception {
            return new Any(element);
        }
    
        @Override
        public Element marshal(Any any) throws Exception {
            return any.element;
        }
    }
    
    @XmlJavaTypeAdapter(AnyAdapter.class)
    class Any {
        Element element;
    
        Any(Element element) {
            this.element = element;
        }
    }
    
  • 0

    我根本不需要任何名称空间 .

    单独使用JAXB将无法实现此目的 . @XmlAnyElement 告诉unmarshaller转储它们可以用它们的命名空间编写的元素 .

    一种选择是使用名称空间不知道的DOM解析器解析传入的XML,然后使用DOM树解组它 . 在Unmarshaller JavaDoc中有一个这样的例子(它使用了一个名称空间感知的解析器;它应该很明显要改变什么来使它成为名称空间 - 不知道) .

    我真的不明白QName的事情

    你的意思是你不明白为什么输出是一个合格的名字,或者为什么它选择了特定的前缀?或者QNames是什么意思?

    它是一个合格的名称,因为这是表示元素最明确的方式 .

    我不能告诉你它为什么选择这个特殊的前缀; JAXP序列化器选择“ns1”,“ns2”等短名称 .

相关问题