gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:52 24 4
gpt4 key购买 nike

我有这个 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;
}


}

编译单元

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;
}

和用于测试的Demo类:

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 文件而只打印其中的一部分?

最佳答案

我没有经常使用 JAXB,但我认为 compilation_unit 字段不需要 @XmlPath("/compilation-unit")。我觉得反序列化xml的时候路径注解和xml不匹配。

关于xml - 我想将一个 xml 文件序列化为 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3424884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com