我有简单的架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="error" type="xs:string">
</xs:element>
</xs:schema>
我使用 JAXB 从 XML Schema 生成 Java 代码。我只有一堂课:
@XmlRegistry
public class ObjectFactory {
private final static QName _Error_QNAME = new QName("", "error");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: error
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "error")
public JAXBElement<String> createError(String value) {
return new JAXBElement<String>(_Error_QNAME, String.class, null, value);
}
}
我通常使用这段代码来解析 XML:
JAXBContext context = JAXBContext.newInstance(RootGenerateClass.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
RootGenerateClass response = (RootGenerateClass) unmarshaller.unmarshal(streamWrapper.getStream());
在这种情况下我该怎么办(我没有任何 rootGenerateClass)?我试试这个:
JAXBContext context = JAXBContext.newInstance(String.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
String response = (String) unmarshaller.unmarshal(streamWrapper.getStream());
当然不行((