如何使JAXB unmarshaller忽略前缀?

时间:2012-08-15 11:21:11

标签: namespaces jaxb prefix unmarshalling

我有以下XML:

<ns2:Person name="John" age="20" />

我想将它解组为从XSD生成的JAXB对象Person。

这是我正在运行的代码:

JAXBContext context = JAXBContext.newInstance(PersoEntity.class);
Unmarshaller um = context.createUnmarshaller();
StringReader sr = new StringReader(xml);
Person p = (Person)um.unmarshal(sr);

令人惊讶的是我得到以下例外:

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: The prefix "ns2" for element "ns2:Person" is not bound.]

我该如何解决?感谢

2 个答案:

答案 0 :(得分:6)

获得片段

您当前获取XML片段的方式导致命名空间声明丢失。在您的片段ns2不再是前缀,您只需要一个带冒号的元素名称(ns2:Person)。这将导致名称空间感知解析器出现问题。下面的文章可能是您获取XML片段的更好方法:

处理您的使用案例

使用您拥有的XML片段,您可以创建一个XMLFilter,从XML元素中删除前缀,然后利用JAXB的UnmarshallerHandler进行解组。

演示

package forum11968399;

import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;

public class Demo {

    private static final String xml = "<ns2:Person name='John' age='20' />";

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xmlReader = sp.getXMLReader();
        XMLFilter xmlFilter = new MyXMLFilter(xmlReader);

        JAXBContext context = JAXBContext.newInstance(PersonEntity.class);
        Unmarshaller um = context.createUnmarshaller();
        UnmarshallerHandler unmarshallerHandler = um.getUnmarshallerHandler();
        xmlFilter.setContentHandler(unmarshallerHandler);

        StringReader sr = new StringReader(xml);
        xmlFilter.parse(new InputSource(sr));
        PersonEntity p = (PersonEntity) unmarshallerHandler.getResult();
    }

    private static class MyXMLFilter extends XMLFilterImpl {

        public MyXMLFilter(XMLReader xmlReader) {
            super(xmlReader);
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            int colonIndex = qName.indexOf(':');
            if(colonIndex >= 0) {
                qName = qName.substring(colonIndex + 1);
            }
uri = XML_NAMESPACE; //to prevent unknown XML element exception, we have to specify the namespace here
            super.startElement(uri, localName, qName, attributes);
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            int colonIndex = qName.indexOf(':');
            if(colonIndex >= 0) {
                qName = qName.substring(colonIndex + 1);
            }
            super.endElement(uri, localName, qName);
        }

    }

}

PersonEntity

package forum11968399;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class PersonEntity {

    @XmlAttribute
    private String name;

    @XmlAttribute
    private int age;

}

答案 1 :(得分:1)

最好的办法是将所需元素嵌套在另一个绑定命名空间的元素中。绑定它并不重要,只需将其作为一个有效的XML文档即可解析。然后你可以unmarshal by declared type

JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller um = context.createUnmarshaller();
String xml = "<ns2:Person name=\"John\" age=\"20\" />";
String xmlWithPrefixMapped = "<ns2:FakeElement xmlns:ns2=\"someuri\">" + xml + "</ns2:FakeElement>";
StringReader sr = new StringReader(xmlWithPrefixMapped);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(sr));
Node n = (Node) doc.getDocumentElement().getFirstChild();
JAXBElement<Person> personElement = um.unmarshal(n, Person.class);
Person p = personElement.getValue();