java 从没有模式的 xml 创建 Jaxb 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13589942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Jaxb classes from xml without schema
提问by Thunderhashy
How Do I create a simple jaxb Java class to represent the following xml
如何创建一个简单的 jaxb Java 类来表示以下 xml
<rootelem>
<myelem name="abc" myatt="true"/>
<myelem name="def">
<Key value="newvalue"/>
</myelem>
<myelem name="xyz">
<Key value="42"/>
</myelem>
</rootelem>
There can be multiple myelem
and each myelem
can contain multiple key
可以有多个myelem
,每个myelem
可以包含多个key
I do not want to use a xsd
我不想使用 xsd
采纳答案by themanatuf
Here is a copy of a class we use to convert to/from XML using JAXB using classes and no XSD. (We also use JAXB to generate our XSD).
这是我们使用 JAXB 使用类而不是 XSD 来转换 XML 和从 XML 转换的类的副本。(我们还使用 JAXB 来生成我们的 XSD)。
EDIT: I just re-read the question. If you're asking how to generate the Java source from that XML, then you're going to have to figure that out on your own or use an XSD and use JAXB to convert it to classes. If you already have the class and you want to convert the XML into a Java object, then my code below will work for you.
编辑:我刚刚重新阅读了这个问题。如果您询问如何从该 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;
}
回答by Evgeniy Dorofeev
Here is a basic example:
这是一个基本示例:
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);
}
}