java - 使用 jaxb 的 XML 解析器

标签 java xml jaxb

如何使用 jaxb 获取嵌套 xml 的对象。我有以下 XML -

<?xml version="1.0" ?>
    <file>
    <markups>
    <markup>
        <author>author</author>
        <name>3w2fg</name>
        <source>Mobile_iPad</source>
        <createdOn>2013-05-20T11:22:23</createdOn>
        <entities>
            <entity>
                <entityWdth>209</entityWdth>
                <entityColor>
                    <red>127.5</red>
                    <green>0</green>
                    <blue>127.5</blue>
                </entityColor>
                <entityFillColor>
                    <red>227.5</red>
                    <green>0</green>
                    <blue>327.5</blue>
                </entityFillColor>
                <entityRadian>0</entityRadian>
                <entityEndY>304</entityEndY>
                <entityStX>438</entityStX>
                <entityTypeCode>7</entityTypeCode>
                <entityPageNo>1</entityPageNo>
                <entityHt>183</entityHt>
                <entityCenterX>542.5</entityCenterX>
                <entityName>Rectangle</entityName>
                <entityStY>121</entityStY>
                <entityEndX>647</entityEndX>
                <entityCenterY>212.5</entityCenterY>
            </entity>
        </entities>
    </markup>
</markups>
<name>7987ab12-4915-49e5-8bbd-f98d6054ef6b.JPG</name>
<fileName>IMG_0008.JPG</fileName>
</file>

我正在使用 jaxb 对其进行解码,如下 -

JAXBContext jbContext = JAXBContext.newInstance(com.arc.markupinfo.generated.File.class);
com.arc.markupinfo.generated.ObjectFactory factory = new    com.arc.markupinfo.generated.ObjectFactory();
com.arc.markupinfo.generated.File fileObj = factory.createFile();
Unmarshaller unmarshaller = jbContext.createUnmarshaller();
fileObj = (com.arc.markupinfo.generated.File) unmarshaller.unmarshal(new   File(xmlLocation));
fileObj.getFileName();

该对象是使用除entityColor.Red ...和entityFillColor.Red ...之外的所有值创建的。这些值是0,0,0,而xml显示它具有正确的值

最佳答案

entityColor.Red ...和entityFillColor.Red ...的类型为int(Integer)。
使用 double (Double)、float (Float) 或 String 作为您的颜色在颜色对象中。

@XmlAccessorType(XmlAccessType.FIELD)
public class Entity 
{
   //...
   private Color entityColor;
   private Color entityFillColor;
   //...
}  

@XmlAccessorType(XmlAccessType.FIELD)
public class Color 
{
   private double red;   // or float, or String
   private double green; // or float, or String
   private double blue;  // or float, or String
}  

您还可以用最简单的方法解码您的示例:

File file = javax.xml.bind.JAXB
   .unmarshal(new java.io.File(xmlLocation),File.class);

关于java - 使用 jaxb 的 XML 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16669098/

相关文章:

xml - 在 xml 中插入属性时获取 'The argument 1 of the xml data type method "修改“必须是字符串文字”

c++ - 将 QPainterPath 写入 XML

java - JAXB 解码 忽略 SOAP 信封/ header 标签

java - @XmlElement 在 kotlin 中使用时不起作用

java - JAXB 映射 将复杂对象映射到 JSON

java - 删除 ArCore 中 anchor 的所有旋转

java - 使用 Java 中的 Floyd Warshall 算法保存图中的最短路径

java - 当我尝试运行一个简单的 Spring 项目时出现错误

java - 使用 POJO 对象类将 JSON 解析为 POJO 对象列表

html - 每次更新站点地图都需要提交给搜索引擎吗?