如何使用XStream库轻松序列化对象中的xml文件

时间:2010-08-04 10:01:35

标签: java xml xstream

我有以下xml文件:

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

    This is a sample netbeans project file for a Squawk project.
    You may edit it freely, it doesn't affect the ant-powered build.

 -->
<project xmlns="http://www.netbeans.org/ns/project/1">
    <type>org.netbeans.modules.ant.freeform</type>
    <configuration>
        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
            <properties>
                <property-file>${user.home}/.sunspot.properties</property-file>
                <property-file>build.properties</property-file>
                <property-file>${sunspot.home}/default.properties</property-file>
            </properties>
            <name>${application.title}</name>
            <folders>
                <source-folder>
                    <label>src</label>
                    <type>java</type>
                    <location>src</location>
                </source-folder>
            </folders>
            <ide-actions>
                <action name="build">
                    <target>jar-app</target>
                </action>
                <action name="run">
                    <target>deploy</target>
                    <target>run</target>
                </action>
                <action name="clean">
                    <target>clean</target>
                </action>
                <action name="rebuild">
                    <target>clean</target>
                    <target>jar-app</target>
                </action>
                <action name="javadoc">
                    <target>javadoc</target>
                </action>
            </ide-actions>
            <export>
                <type>folder</type>
                <location>build</location>
                <build-target>jar-app</build-target>
            </export>
            <view>
                <items>
                    <source-folder style="packages">
                        <label>src</label>
                        <location>src</location>
                    </source-folder>
                    <source-file>
                        <location>build.xml</location>
                    </source-file>
                </items>
                <context-menu>
                    <ide-action name="build"/>
                    <ide-action name="rebuild"/>
                    <ide-action name="clean"/>
                    <ide-action name="javadoc"/>
                    <ide-action name="run"/>
                    <action>
                        <label>Build Project + Deploy to Sun SPOT</label>
                        <target>deploy</target>
                    </action>
                    <action>
                        <label>Deploy to Sun SPOT</label>
                        <target>jar-deploy</target>
                    </action>
                    <separator/>
                </context-menu>
            </view>
            <subprojects/>
        </general-data>
        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
            <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>
        </java-data>
        <preferences xmlns="http://www.netbeans.org/ns/auxiliary-configuration-preferences/1">
            <module name="org-netbeans-modules-editor-indent">
                <node name="CodeStyle">
                    <property name="usedProfile" value="default"/>
                    <node name="project">
                        <property name="tab-size" value="8"/>
                        <property name="text-limit-width" value="80"/>
                    </node>
                </node>
                <node name="text">
                    <node name="x-java">
                        <node name="CodeStyle">
                            <node name="project"/>
                        </node>
                    </node>
                </node>
            </module>
        </preferences>
    </configuration>
</project>

我想用java和Xstream库(序列化)解析它,以便更改某些属性和节点的值,然后在文件上取消激活它。但是我发现将java类设置为难以实现并不方便处理所有这些节点(我必须为每个节点创建一个java类,例如:1个类,然后是1个类,等等...)。我发现它有点浪费时间。有没有其他方法可以更快地完成它更少的代码。也许我理解库的工作方式有什么不对吗? 感谢

2 个答案:

答案 0 :(得分:0)

如果要控制用于序列化的XML(或已定义XML模式),则可以实现转换器类。 但是,如果你接受默认编码(或者只是略微调整它),XStream最容易使用。然后你需要很少的编码 - 但你必须接受它生成的XML。

答案 1 :(得分:0)

不确定您是如何与XStream绑定的,但MOXy JAXB提供了您正在寻找的映射类型。下面是您的对象如何映射的部分样本:

package-info.java(设置包级别注释)

此处指定的名称空间前缀将在稍后使用。

@XmlSchema( 
    namespace = "http://www.netbeans.org/ns/project/1", 
    xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix = "f", namespaceURI = "http://www.netbeans.org/ns/freeform-project/1"),
        },
    elementFormDefault = XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
package example; 

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

<强> Project.java

注意使用@XmlPath来打破类和节点之间的1对1依赖关系。

package example;

import java.util.List;

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

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

@XmlRootElement
@XmlType(propOrder={"type", "propertyFile", "name"})
public class Project {

    private String type;

    @XmlPath("configuration/f:general-data/f:properties/f:property-file/text()")
    private List<String> propertyFile;

    @XmlPath("configuration/f:general-data/f:name/text()")
    private String name;

    @XmlPath("configuration/f:general-data/f:folders/f:source-folder")
    private List<Folder> sourceFolder;

    @XmlPath("configuration/f:general-data/f:ide-actions/f:action")
    private List<Action> action;

}

<强> Folder.java

package example;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace="http://www.netbeans.org/ns/freeform-project/1")
public class Folder {

    private String label;
    private String type;
    private String location;

}

<强> Action.java

package example;

import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(namespace="http://www.netbeans.org/ns/freeform-project/1")
public class Action {

    @XmlAttribute
    private String name;

    private List<String> target;
}

<强> Demo.java

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("src/forum32/input.xml");
        Project project = (Project) jc.createUnmarshaller().unmarshal(xml);

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

要使用MOXy JAXB,您需要使用以下条目在模型类中添加jaxb.properties文件:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory