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 建议,布尔 requirePrefix) { return ""; } };

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

  • 我真的不明白 QName 的事情

4

2 回答 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;
    }
}
于 2013-02-07T21:38:53.717 回答
0

我根本不想要任何命名空间。

您将无法单独使用 JAXB 完成此操作。告诉解组器将@XmlAnyElement它无法处理的元素转储到您的列表中。这些元素附加了命名空间。然后,当您对这些元素进行编组时,将使用它们的命名空间来编写它们。

一种选择是使用不知道名称空间的 DOM 解析器解析传入的 XML,然后使用 DOM 树解组它。UnmarshallerJavaDoc中有一个这样的例子(它使用了一个命名空间感知解析器;应该很明显改变什么让它不知道命名空间)。

我真的不明白 QName 的事情

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

这是一个限定名称,因为这是表示元素的最明确的方式。

我不能告诉你为什么它选择了这个特定的前缀;JAXP 序列化程序选择短名称,如“ns1”、“ns2”等。

于 2013-02-07T21:43:12.523 回答