tags:

views:

71

answers:

2

I have the below xml file:

    <?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"&gt;
    <type>org.netbeans.modules.ant.freeform</type>
    <configuration>
        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1"&gt;
            <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"&gt;
            <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"&gt;
            <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>

I want to parse it with java and the Xstream library(serialize) in order to change the values of some properties and nodes and then deserilaze it on a file.But i find it difficult and not so convenient to make java classes to handle all these nodes(I have to make a java class for each node e.g:1 class for then 1 class for etc...).I found it a little bit waste of time.Is there any other way to do it faster with less code.Or maybe did i understand something wrong at the way that the library is working? Thanks

A: 

You can implement converter classes if you want to control the XML used for serializing (or have the XML schema already defined). But XStream is easiest to use if you accept the default coding (or maybe just adapt it slightly). Then you need few coding - but you have to accept the XML it generates.

Frank
Yes but to implement a converter i have to specify the object that will handle the xml file.My question is :The object must follow the xml schema completely?Can i have an object only with the values that i am interesting in,for later changing them?
herc
AFter reading the FAQ of Xstream (http://xstream.codehaus.org/faq.html#Uses) i answer to my own question."Can XStream generate classes from XSD?No. For this kind of work a data binding tool such as XMLBeans is appropriate."So i used the tools of XMLBeans to generate xsd then create classes based on that xsd.
herc
A: 

Not sure how tied to XStream you are, but MOXy JAXB offers the type of mapping you are looking for. Below is a partial sampling of how your object could be mapped:

package-info.java (to set package level annotations)

The namespace prefixes assigned here will be used later.

@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

Notice the use of @XmlPath to break the 1-to-1 dependency between classes and nodes.

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

To use MOXy JAXB you need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Blaise Doughan
thanks for response.I find it useful and i will have a look at it.But i am searching for something that will do this job automatically.And i think that what i am searching for is xmlbean.Xmlbean will create the schema from the xml and then automatically will create java classes to bind the xml into an object.But i think that xmlbean is quite more complex.I do not know what to choose yet...
herc
Also did the java code has to include every element and node of your xml file?I mean that the code below will work even if it is not completed with all the appropriate code to hold all the nodes and elements of my example.xml file?
herc
The package-info.java file should be excluded from compilation.Right?
herc
package-info.java should be included as part of the compilation. Refer to http://onjava.com/pub/a/onjava/2004/04/21/declarative.html?page=3
Blaise Doughan
You can also generate classes from an XML schema using JAXB (included in Java SE 6). The classes produced will be annotated POJOs (plain old java objects). By default JAXB can handle missing and extra nodes without throwing errors. If you want JAXB to operate in a strict mode you just set a schema object on the unmarshaller.
Blaise Doughan
I am using IntelliJ IDEA as my java framework and i am getting an illegal start of expression exception at package-info.java at the end of that line:@javax.xml.bind.annotation.XmlNs(prefix = "f", namespaceURI = "http://www.netbeans.org/ns/freeform-project/1"), My package-info.java file is exactly the same as the one you posted at the beginning
herc
Ok ,the problem was the comma an the end
herc