我想将 xml 文件序列化为 java 对象

发布于 2024-09-12 20:11:54 字数 4187 浏览 3 评论 0原文

我有这个 xml 文件,我想将其解压缩到 java 对象中。我正在使用 MOXy JAXB 库。

test.xml

    <?xml version="1.0" encoding="UTF-8"?>

<project >
    <type>org.netbeans.modules.ant.freeform</type>
            <compilation-unit>
                <package-root>src</package-root>
                <classpath mode="boot">${sunspot.bootclasspath}</classpath>
                <classpath mode="compile">${sunspot.classpath}</classpath>
                <built-to>build</built-to>
                <source-level>1.4</source-level>
            </compilation-unit>

</project>

这是我的 java 类: Project.java

package example;

import java.util.List;

import javax.xml.bind.annotation.*;


import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlType(name = "project",propOrder = {"type", "compilation_unit"})
public class Project {
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private String type;


    @XmlPath("/compilation-unit")
    @XmlElement(name = "compilation-unit")
    private CompilationUnit compilation_unit;

    public CompilationUnit getPckg() {
        return compilation_unit;
    }

    public void setPckg(String pckg) {
        this.compilation_unit = compilation_unit;
    }


}

CompilationUnit

package example;

import org.eclipse.persistence.oxm.annotations.XmlPath;

import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ronny
 * Date: Aug 5, 2010
 * Time: 11:28:37 AM
 * To change this template use File | Settings | File Templates.
 */
@XmlType(name = "compilation-unit", propOrder = {"package_root","built_to" ,"source_level" ,"classpath"})

public class CompilationUnit {
    public String getPackage_root() {
        return package_root;
    }

    public void setPackage_root(String package_root) {
        this.package_root = package_root;
    }

    public String getBuilt_to() {
        return built_to;
    }

    public void setBuilt_to(String built_to) {
        this.built_to = built_to;
    }

    public String getSource_level() {
        return source_level;
    }

    public void setSource_level(String source_level) {
        this.source_level = source_level;
    }

    public List<Classpath> getClasspath() {
        return classpath;
    }

    public void setClasspath(List<Classpath> classpath) {
        this.classpath = classpath;
    }

    private String package_root;
    private String built_to;
    private String source_level;
    private List<Classpath> classpath;

}

类路径:

package example;

import javax.xml.bind.annotation.XmlAttribute;

/**
 * Created by IntelliJ IDEA.
 * User: leontiad
 * Date: Aug 5, 2010
 * Time: 11:33:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class Classpath {
    @XmlAttribute
    private String mode;
}

以及用于测试的演示类:

package example;

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

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

        FileInputStream xml = new FileInputStream("C:\\task.xml");
        Project project = (Project) jc.createUnmarshaller().unmarshal(xml);

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

以及我得到的结果输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<project>
   <type>org.netbeans.modules.ant.freeform</type>
</project>

任何人都可以帮助理解为什么不打印整个 xml 文件而只打印其中的一部分吗?

I have this xml file which i want to unmarchal it in a java object.I am using the MOXy JAXB library.

test.xml

    <?xml version="1.0" encoding="UTF-8"?>

<project >
    <type>org.netbeans.modules.ant.freeform</type>
            <compilation-unit>
                <package-root>src</package-root>
                <classpath mode="boot">${sunspot.bootclasspath}</classpath>
                <classpath mode="compile">${sunspot.classpath}</classpath>
                <built-to>build</built-to>
                <source-level>1.4</source-level>
            </compilation-unit>

</project>

Here is my java classes:
Project.java

package example;

import java.util.List;

import javax.xml.bind.annotation.*;


import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlType(name = "project",propOrder = {"type", "compilation_unit"})
public class Project {
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private String type;


    @XmlPath("/compilation-unit")
    @XmlElement(name = "compilation-unit")
    private CompilationUnit compilation_unit;

    public CompilationUnit getPckg() {
        return compilation_unit;
    }

    public void setPckg(String pckg) {
        this.compilation_unit = compilation_unit;
    }


}

CompilationUnit

package example;

import org.eclipse.persistence.oxm.annotations.XmlPath;

import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ronny
 * Date: Aug 5, 2010
 * Time: 11:28:37 AM
 * To change this template use File | Settings | File Templates.
 */
@XmlType(name = "compilation-unit", propOrder = {"package_root","built_to" ,"source_level" ,"classpath"})

public class CompilationUnit {
    public String getPackage_root() {
        return package_root;
    }

    public void setPackage_root(String package_root) {
        this.package_root = package_root;
    }

    public String getBuilt_to() {
        return built_to;
    }

    public void setBuilt_to(String built_to) {
        this.built_to = built_to;
    }

    public String getSource_level() {
        return source_level;
    }

    public void setSource_level(String source_level) {
        this.source_level = source_level;
    }

    public List<Classpath> getClasspath() {
        return classpath;
    }

    public void setClasspath(List<Classpath> classpath) {
        this.classpath = classpath;
    }

    private String package_root;
    private String built_to;
    private String source_level;
    private List<Classpath> classpath;

}

Classpath:

package example;

import javax.xml.bind.annotation.XmlAttribute;

/**
 * Created by IntelliJ IDEA.
 * User: leontiad
 * Date: Aug 5, 2010
 * Time: 11:33:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class Classpath {
    @XmlAttribute
    private String mode;
}

and the Demo class for testing:

package example;

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

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

        FileInputStream xml = new FileInputStream("C:\\task.xml");
        Project project = (Project) jc.createUnmarshaller().unmarshal(xml);

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

and what i am getting as output is the following:

<?xml version="1.0" encoding="UTF-8"?>
<project>
   <type>org.netbeans.modules.ant.freeform</type>
</project>

Could anyone help to understand why the whole xml file is not printed and only a portion of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-09-19 20:11:54

我没有太多使用JAXB,但我认为compilation_unit字段不需要@XmlPath(“/compilation-unit”)。我认为当xml反序列化时,路径注释与xml不匹配。

I have not used JAXB much, but I think the compilation_unit field does not need the @XmlPath("/compilation-unit"). I think when the xml is deserialized, the path annotation does not match the xml.

困倦 2024-09-19 20:11:54

您只是缺少几个注释,下面是更正后的类:

CompilationUnit

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import java.util.List; 

/** 
 * Created by IntelliJ IDEA. 
 * User: leontiad 
 * Date: Aug 5, 2010 
 * Time: 11:28:37 AM 
 * To change this template use File | Settings | File Templates. 
 */ 
@XmlType(name = "compilation-unit", propOrder = {"package_root","built_to" ,"source_level" ,"classpath"}) 
@XmlAccessorType(XmlAccessType.FIELD)
public class CompilationUnit { 
    public String getPackage_root() { 
        return package_root; 
    } 

    public void setPackage_root(String package_root) { 
        this.package_root = package_root; 
    } 

    public String getBuilt_to() { 
        return built_to; 
    } 

    public void setBuilt_to(String built_to) { 
        this.built_to = built_to; 
    } 

    public String getSource_level() { 
        return source_level; 
    } 

    public void setSource_level(String source_level) { 
        this.source_level = source_level; 
    } 

    public List<Classpath> getClasspath() { 
        return classpath; 
    } 

    public void setClasspath(List<Classpath> classpath) { 
        this.classpath = classpath; 
    } 

    @XmlElement(name="package-root") 
    private String package_root;

    @XmlElement(name="built-to")
    private String built_to; 

    @XmlElement(name="source-level")
    private String source_level; 
    private List<Classpath> classpath; 

}

Project

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement 
@XmlType(name = "project",propOrder = {"type", "compilation_unit"}) 
@XmlAccessorType(XmlAccessType.FIELD)
public class Project { 
    public String getType() { 
        return type; 
    } 

    public void setType(String type) { 
        this.type = type; 
    } 

    private String type; 

    @XmlElement(name = "compilation-unit") 
    private CompilationUnit compilation_unit; 

    public CompilationUnit getPckg() { 
        return compilation_unit; 
    } 

    public void setPckg(String pckg) { 
        this.compilation_unit = compilation_unit; 
    } 

}

仅供参考,下面是一篇关于 @XmlPath 注释的文章:

You were just missing a couple of annotations, below are the corrected classes:

CompilationUnit

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import java.util.List; 

/** 
 * Created by IntelliJ IDEA. 
 * User: leontiad 
 * Date: Aug 5, 2010 
 * Time: 11:28:37 AM 
 * To change this template use File | Settings | File Templates. 
 */ 
@XmlType(name = "compilation-unit", propOrder = {"package_root","built_to" ,"source_level" ,"classpath"}) 
@XmlAccessorType(XmlAccessType.FIELD)
public class CompilationUnit { 
    public String getPackage_root() { 
        return package_root; 
    } 

    public void setPackage_root(String package_root) { 
        this.package_root = package_root; 
    } 

    public String getBuilt_to() { 
        return built_to; 
    } 

    public void setBuilt_to(String built_to) { 
        this.built_to = built_to; 
    } 

    public String getSource_level() { 
        return source_level; 
    } 

    public void setSource_level(String source_level) { 
        this.source_level = source_level; 
    } 

    public List<Classpath> getClasspath() { 
        return classpath; 
    } 

    public void setClasspath(List<Classpath> classpath) { 
        this.classpath = classpath; 
    } 

    @XmlElement(name="package-root") 
    private String package_root;

    @XmlElement(name="built-to")
    private String built_to; 

    @XmlElement(name="source-level")
    private String source_level; 
    private List<Classpath> classpath; 

}

Project

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement 
@XmlType(name = "project",propOrder = {"type", "compilation_unit"}) 
@XmlAccessorType(XmlAccessType.FIELD)
public class Project { 
    public String getType() { 
        return type; 
    } 

    public void setType(String type) { 
        this.type = type; 
    } 

    private String type; 

    @XmlElement(name = "compilation-unit") 
    private CompilationUnit compilation_unit; 

    public CompilationUnit getPckg() { 
        return compilation_unit; 
    } 

    public void setPckg(String pckg) { 
        this.compilation_unit = compilation_unit; 
    } 

}

FYI, below is an article on the @XmlPath annotation:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文