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

标签 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个类等...)。我发现这有点浪费时间。有没有其他方法可以更快地做到这一点更少的代码。或者也许我对库的工作方式理解有问题? 谢谢

最佳答案

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

关于java - 如何使用 XStream 库轻松序列化对象中的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3404440/

相关文章:

java - XStream XmlPullParserException

java - 与mysql中的整数值进行比较

Java 保龄球程序 - 添加第 10 帧

javascript - 将任何 XML 或 JS 对象显示为树

java - 如何在 android studio 中的给定 XML 布局文件中使用 Java 动态添加按钮?

java - 使用 XStream 解析变化的 XML

java - C# 序列化器/反序列化器与 java 中的 XStream 具有相同的功能

java - 如何通过 RServe 将浮点/ double 组作为 vector 从 JAVA 发送到 R

java - RxJava v1 到 v2

java - 如何在不使用 xml 或注释的情况下使 Spring 识别 bean?