jaxb unMarshaller失败ClassCastException,因为两个xml元素的名称相同。为什么?

时间:2016-03-26 18:47:47

标签: java xml jaxb unmarshalling

我的unmarshaller遇到了问题。我有一个如下所示的文件:

legend

问题是根元素,元素列表的名称是“Employee”。当我去解组时,我得到了一个classcastexception。

a

我能够使用此代码来编组一个看起来与我需要解组的文件完全相同的文件。所以我很困惑。什么是遗失,所以当我解组时,unmarshaller没有给我以下例外:

% Plot something
h = plot((0:.1:2*pi),sin((0:.1:2*pi)),'o-',(0:.1:2*pi),cos((0:.1:2*pi)),'r+-');
% Add the legend
[a,b,icons,c] = legend(h,'Data1','Data2');
% Add an axes to the figure
ax=axes;
% Enlarge the legend, then set the axes position and size equal to the
% legend box
%Get the legend's position and size
ax_p=a.Position;
a.Position=[ax_p(1)-.2 ax_p(2) ax_p(3)+.2 ax_p(4)];
ax.Position=a.Position;
ax.Units='normalized';
ax.Box='on';
% Plot the firt line in the axes
plot(ax,b(3).XData,b(3).YData,'color',b(3).Color);
hold on
% Add the marker of the first line at the end of the line
plot(ax,b(3).XData(end),b(3).YData(end), ...
             'marker',b(4).Marker, ...
             'markeredgecolor',b(4).Color, ...
             'markerfacecolor',b(3).MarkerFaceColor);
% Get second line XData and YData
x=b(5).XData;
y=b(5).YData;
% Define the number of line sections
n=5;
% Update the XData and YData by defning intermediate values
len=linspace(x(1),x(2),n);
% Plot the set of line with different colours
for i=1:n-1
   plot(ax,[len(i) len(i+1)],[y(2) y(2)], ...
      'marker',b(6).Marker,'markeredgecolor',b(6).Color, ...
      'markerfacecolor',b(6).MarkerFaceColor, ...
      'color',rand(1,3),'linewidth',1.5);
end
% Get the legend texts position
pt1=b(1).Position;
pt2=b(2).Position;
% Add the legend text
text(pt1(1)+.1,pt1(2),a.String{1});
text(pt2(1)+.1,pt2(2),a.String{2});
% Remove the axes ticks
ax.XTick=[];
ax.YTick=[];
% Set the axes limits
ax.XLim=[0 1];
ax.YLim=[0 1];

1 个答案:

答案 0 :(得分:0)

无法重现(在Java 1.8.0_65上测试)。

由于您没有提供MCVE(最小,完整和可验证的示例),因此这是一个有效的。

只有已知的区别在于删除了名称空间以进行简单测试。

import java.io.StringReader;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Test {
    public static void main(String[] args) throws Exception {
        String xml = "<Employee>\r\n" +
                     "<Employee>\r\n" +
                     "    <Id>2</Id>\r\n" +
                     "    <Name>idk</Name>\r\n" +
                     "</Employee>\r\n" +
                     "</Employee>\r\n";
        JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeInformation.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        EmployeeInformation empInfo = (EmployeeInformation)unmarshaller.unmarshal(new StringReader(xml));
        System.out.println(empInfo);
    }
}
@XmlRootElement(name="Employee")
class EmployeeInformation {

    private List<EmployeeInformationElement> elements;

    @XmlElement(name="Employee")
    public List<EmployeeInformationElement> getElements() {
        return elements;
    }
    public void setElements(List<EmployeeInformationElement> elements) {
        this.elements = elements;
    }
}
class EmployeeInformationElement {

    private int id;
    private String name;

    @XmlElement(name="Id")
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @XmlElement(name="Name")
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}