У меня есть следующий код (работает ОК), и я должен закрепить его, добавив некоторые ограничения в процесс Unmarshaling < /p>
public static Response getObjectBinResponse1(String xml) throws JAXBException{
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
Response rsp=(Response) unmarshaller.unmarshal(reader);
reader.close();
return rsp;
}
< /code>
Я попробовал это: < /p>
public static Response getObjectBinResponse2(String xml) throws JAXBException, ParserConfigurationException, SAXException, UnsupportedEncodingException{
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
spf.setFeature("http://apache.org/xml/features/nonvalid ... ternal-dtd", false);
spf.setFeature("http://xml.org/sax/features/validation", false);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource(new StringReader(xml));
SAXSource source = new SAXSource(xmlReader, inputSource);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response rsp=(Response) unmarshaller.unmarshal(source);
return rsp;
}
< /code>
Но этот код повышает следующую ошибку: < /p>
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ns0:Response"). Expected elements are
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
< /code>
Я попробовал код этого ответа, изменив исходный файл на строку. />
Имя класса верно < /p>
@XmlRootElement(name = "Response")
public class Response {
< /code>
У меня есть пакет файлов info.java правильный < /p>
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xxx.yy/aaa/vbb/v1")
package my.generated.package.from.xsd.with.jaxb;
< /code>
и строка XML выглядит как < /p>
String xml="
Если я изменю эту строку < /p>
SAXSource source = new SAXSource(xmlReader, inputSource);
< /code>
к этому < /p>
SAXSource source = new SAXSource(inputSource);
< /code>
работает, но мне нужен Xmlreader из -за ограничений < /p>
Подробнее здесь: https://stackoverflow.com/questions/580 ... ring-input