如何在JaxB中忽略元素名称的大小写

时间:2010-12-07 09:36:25

标签: java xml jaxb

如标题中所述,我想忽略文档中元素名称的大小写。

static class XY433 {
    @XmlAttribute(name = "C200")
    String c200;
    @XmlAttribute(name = "C215")
    String c215;

    @XmlAttribute(name="F001")
    String f001;

    @XmlAttribute(name="f001")
    String lcf001; // I want to avoid this duplication
}

我试图使用Blaise Doughan发布的代码:

private static class ToLowerCaseNamesStreamReaderDelegate extends StreamReaderDelegate {

    public ToLowerCaseNamesStreamReaderDelegate(XMLStreamReader xsr) {
        super(xsr);
    }

    @Override
    public String getAttributeLocalName(int index) {
        return super.getAttributeLocalName(index).toLowerCase();
    }

    @Override
    public String getLocalName() {
        return super.getLocalName().toLowerCase();
    }

}


@XmlRootElement(name="doc")
static class Doc {
    @XmlElement(name="element")
    List<Element> elements;
}

static class Element {
    @XmlAttribute(name = "abc")
    String abc;
}

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("LowerCaseElementNamesFilterTest.xml"));

    Unmarshaller u = JAXBContext.newInstance(Doc.class).createUnmarshaller();

    //Do unmarshalling
    Doc doc = (Doc) u.unmarshal(new ToLowerCaseNamesStreamReaderDelegate(xsr));

    System.out.println(doc.elements.get(0).abc);
    System.out.println(doc.elements.get(1).abc);
    System.out.println(doc.elements.get(2).abc);

}

这实际上不起作用。

null
2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.RangeCheck(ArrayList.java:546)
    at java.util.ArrayList.get(ArrayList.java:321)
    at com.hre.commons.tec.xml.LowerCaseElementNamesFilter.main(LowerCaseElementNamesFilter.java:58)

对于这个XML:

<doc>
    <Element ABC="1"></Element>
    <element Abc="1"></element>
    <element abc="2"></element>
</doc>

3 个答案:

答案 0 :(得分:9)

您可以将所有属性映射到小写节点名称,然后包装XMLStreamReader以在它返回的所有属性/元素名称上调用toLowerCase()。然后从该XMLStreamReader解组。

我最近为此问题添加了EclipseLink JAXB(MOXy)的增强请求,请随时提供其他信息:

对象模型

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private int id;
    private String name;
    private Address address;

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}


public class Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

演示代码

import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("input.xml"));
        xsr = new MyStreamReaderDelegate(xsr);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

    private static class MyStreamReaderDelegate extends StreamReaderDelegate {

        public MyStreamReaderDelegate(XMLStreamReader xsr) {
            super(xsr);
        }

        @Override
        public String getAttributeLocalName(int index) {
            return super.getAttributeLocalName(index).toLowerCase();
        }

        @Override
        public String getLocalName() {
            return super.getLocalName().toLowerCase();
        }

    }

}

将阅读这些XML文档:

<CUSTOMER ID="1">
    <NAME>Jane Doe</NAME>
    <ADDRESS>
        <STREET>123 A Street</STREET>
    </ADDRESS>
</CUSTOMER>

<CuSToMeR Id="1">
    <NaMe>Jane Doe</NAME>
    <AdDrEsS>
        <STREET>123 A Street</STREET>
    </AdDRrEsS>
</CuSToMeR>

并编写以下XML:

<customer id="1">
   <address>
      <street>123 A Street</street>
   </address>
   <name>Jane Doe</name>
</customer>

以下是更详细示例的链接:

<强>更新

您的代码在我的环境中工作(包含JAXB和EclipseLink JAXB(MOXy)2.2的JDK 1.6.0_20,我也使用StAX的默认实现)。当我运行你的例子时:

import java.io.FileInputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class Example {

    private static class ToLowerCaseNamesStreamReaderDelegate extends StreamReaderDelegate {

        public ToLowerCaseNamesStreamReaderDelegate(XMLStreamReader xsr) {
            super(xsr);
        }

        @Override
        public String getAttributeLocalName(int index) {
            return super.getAttributeLocalName(index).toLowerCase();
        }

        @Override
        public String getLocalName() {
            return super.getLocalName().toLowerCase();
        }

    }


    @XmlRootElement(name="doc")
    static class Doc {
        @XmlElement(name="element")
        List<Element> elements;
    }

    static class Element {
        @XmlAttribute(name = "abc")
        String abc;
    }

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("LowerCaseElementNamesFilterTest.xml"));

        Unmarshaller u = JAXBContext.newInstance(Doc.class).createUnmarshaller();

        //Do unmarshalling
        Doc doc = (Doc) u.unmarshal(new ToLowerCaseNamesStreamReaderDelegate(xsr));

        System.out.println(doc.elements.get(0).abc);
        System.out.println(doc.elements.get(1).abc);
        System.out.println(doc.elements.get(2).abc);

    }
}

我得到以下输出:

1
1
2

更新#2

解决:

  

线程“main”中的异常   javax.xml.bind.UnmarshalException -   链接异常:   [javax.xml.bind.UnmarshalException:   名称空间URI和本地名称   unmarshaller需要被拘禁。] at   com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:425)   在   com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)   在   com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)

您是否尝试过修改代理?:

@Override
public String getAttributeLocalName(int index) {
    return super.getAttributeLocalName(index).toLowerCase().intern();
}

@Override
public String getLocalName() {
    return super.getLocalName().toLowerCase().intern();
}

答案 1 :(得分:1)

遇到类似的问题。 我的解决方案是在模型实体上添加@XmlAdpater,它引用您自己的XMLAdapter,这将在marshall / unmarshall上提供灵活的自定义。

答案 2 :(得分:1)

我正在使用moxy 2.7.1版本,它可以与UnmarshallerProperty一起使用,如下所示。

unmarshaller.setProperty(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE, true);

eclipselink documentation