java JAXB 解组 <string>foobar</string>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1581290/
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
JAXB to unmarshall <string>foobar</string>
提问by laz
Greetings! I have a server returning XML content to my client that looks like this:
问候!我有一个服务器向我的客户端返回 XML 内容,如下所示:
<string xmlns="...">foobar</string>
I'm new to JAXB and have gotten a lot of stuff working, except this. I thought it would be pretty easy to marshal and unmarshal this to and from a String. It took a while, but I finally figured out how to marshal this as
我是 JAXB 的新手,除了这个之外,我已经做了很多工作。我认为将它编组和解组到字符串和从字符串中解组会很容易。花了一段时间,但我终于想出了如何将其编组为
public static String ToXML(String s) throws Exception
{
JAXBContext context = JAXBContext.newInstance(String.class);
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(new JAXBElement(new QName("", "String"), String.class, s), sw);
return sw.toString();
}
So my question is, how to I unmarshal this? It cannot be annotated as a root element. I cannot use java.lang as the package to create a new instance of a JAXBContext (I get an ObjectFactory missing exception).
所以我的问题是,如何解组它?它不能被注释为根元素。我不能使用 java.lang 作为包来创建 JAXBContext 的新实例(我得到一个 ObjectFactory 丢失异常)。
Any wisdom to impart? This can't be that hard, right?
有什么智慧可以传授吗?这不可能那么难,对吧?
回答by skaffman
You need to write an object model that conforms to your XML structure, and tell JAXB to unmarshal on to that. Your example may look simple, but it's not what JAXB is for.
您需要编写一个符合您的 XML 结构的对象模型,并告诉 JAXB 对其进行解组。您的示例可能看起来很简单,但这不是 JAXB 的用途。
Try something like this:
尝试这样的事情:
@XmlRootElement(name="string", namespace="blah")
public class MyString {
@XmlValue
String value;
}
JAXBContext context = JAXBContext.newInstance(MyString.class);
MyString myString = (MyString) context.createUnmarshaller().unmarshal(...);
This will unmarshal the XML <string xmlns="blah">foobar</string>. Change the namespace accordingly. If you have many namespaces, then JAXB isn't really the tool for you.
这将解组 XML <string xmlns="blah">foobar</string>。相应地更改命名空间。如果您有许多名称空间,那么 JAXB 并不是真正适合您的工具。
回答by laz
I was surprised by this, but the following seems to work:
我对此感到惊讶,但以下似乎有效:
final String xmlString = "<string>value</string>";
final StringReader xmlReader = new StringReader(xmlString);
final StreamSource xmlSource = new StreamSource(xmlReader);
final JAXBContext jaxbContext = JAXBContext.newInstance(String.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
final String stringValue = unmarshaller.unmarshal(xmlSource, String.class).getValue();
Is that what you were looking for?
这就是你要找的吗?
回答by Andy Gherna
When you're using JAXB, you need to build the code around an XML schema. That is, if you have a file, say foo.xsd, you need to run it through the xjc compiler (in JDK 6 by default, otherwise you can download JAXB 2 and use that). That will read through the schema and generate the Java bean and associated ObjectFactory classes with the elements in the schema. The Java bean classes will look like regular POJOs with annotations. The ObjectFactory classes are needed by the JAXB implementation to convert the XML into the corresponding Java bean. This explains your ObjectFactory missing exception.
当您使用 JAXB 时,您需要围绕 XML 模式构建代码。也就是说,如果你有一个文件,比如 foo.xsd,你需要通过 xjc 编译器运行它(默认情况下在 JDK 6 中,否则你可以下载 JAXB 2 并使用它)。这将通读模式并使用模式中的元素生成 Java bean 和关联的 ObjectFactory 类。Java bean 类看起来像带有注释的常规 POJO。JAXB 实现需要 ObjectFactory 类来将 XML 转换为相应的 Java bean。这解释了您的 ObjectFactory 缺少异常。
So it's not hard, but there is some leg work involved. We use it for one of our production applications and it's great. I see myself using it more now that it's part of the JDK.
所以这并不难,但涉及到一些腿部工作。我们将它用于我们的生产应用程序之一,它很棒。现在我看到自己更多地使用它,因为它是 JDK 的一部分。

