2012-11-27 87 views
3

如何创建一个简单的JAXB的Java类来表示下面的XML
从XML创建JAXB类,而架构

<rootelem> 
    <myelem name="abc" myatt="true"/> 

    <myelem name="def"> 
    <Key value="newvalue"/> 
    </myelem> 
    <myelem name="xyz"> 
    <Key value="42"/> 
    </myelem> 
</rootelem> 

可以有多个myelem每个myelem可以包含多个key

我做不想使用xsd

+1

但是你可以生成像XML实例的XSD。 –

+0

以下示例应该有所帮助:http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted –

回答

1

以下是我们用来使用JAXB转换为/从XML转换为XML的类的副本,没有使用XSD。 (我们也使用JAXB来生成我们的XSD)。

编辑:我只是重读这个问题。如果您要问如何从该XML中生成Java源代码,那么您将不得不自己弄清楚,或者使用XSD并使用JAXB将其转换为类。如果您已经拥有该类,并且想要将XML转换为Java对象,那么我的代码将适用于您。

package com.mycompany.types; 

import java.io.StringReader; 
import java.io.StringWriter; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlTransient; 

/** 
* Utility class to make it convenient to marshal and unmarshal the classes 
* generated by JAXB. 
*/ 
@XmlTransient 
public final class Utility { 

    // 
    // Static initialization 
    // 

    static { 
    try { 
     JAXB_CONTEXT = JAXBContext.newInstance(TestClass.class); 
// The following fails with a javax.xml.bind.JAXBException. 
// class mycompany.types.TestClass nor any of its super class is known 
// to this context. 
//  JAXB_CONTEXT = 
//  JAXBContext.newInstance("com.mycompany.types", 
//        Utility.class.getClassLoader()); 
    } 
    catch (Exception e) { 
     throw new ExceptionInInitializerError(e); 
    } 
    } 

    // 
    // Constructors 
    // 

    // 
    // Hidden constructor that prevents an object from being created. 
    // 
    private Utility() { 
    // Do nothing. 
    } 

    // 
    // Additional methods 
    // 

    /** 
    * Unmarshals an XML string to a TestClass object. 
    * 
    * @param xml the XML string to parse 
    * @return the resulting TestClass 
    * @throws JAXBException if there are XML errors 
    */ 
    public static TestClass parseTestClass(String xml) throws JAXBException { 
    Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); 
    return (TestClass)unmarshaller.unmarshal(new StringReader(xml)); 
    } 

    /** 
    * Marshals a TestClass object to an XML string. 
    * 
    * @param testClass 
    * @return the resulting XML string 
    * @throws JAXBException if there are XML errors 
    */ 
    public static String printTestClass(TestClass testClass) throws JAXBException { 
    Marshaller marshaller = JAXB_CONTEXT.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    StringWriter writer = new StringWriter(); 
    marshaller.marshal(testClass, writer); 
    return writer.toString(); 
    } 

    // 
    // Attributes 
    // 

    private static final JAXBContext JAXB_CONTEXT; 

} 
4

这是一个基本的例子:

import java.io.FileReader; 
import java.util.List; 

import javax.xml.bind.JAXB; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="rootelem") 
class RootElem { 
    List<MyElem> myelem; 
} 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="myelem") 
class MyElem { 
    @XmlAttribute 
    String name; 
    @XmlAttribute 
    Boolean myatt; 
    @XmlElement(name="Key") 
    List<Key> keys; 
} 

@XmlAccessorType(XmlAccessType.FIELD) 
class Key { 
    @XmlAttribute 
    String value; 
} 

public class Test1 { 

    public static void main(String[] args) throws Exception { 
     RootElem r = JAXB.unmarshal(new FileReader("test.xml"), RootElem.class); 
     System.out.println(r); 
     JAXB.marshal(r, System.out); 
    } 
} 
+0

如果myelem将被包装会怎样影响您的答案? – Dejell