我正在尝试使用JAXB以下列格式解组XML:
<return_data api_call="getuser">
<users>
<user>
<username>test_me</username>
<account_expiration_date>0000-00-00</account_expiration_date>
</user>
</users>
</return_data>
我想从用户元素开始解组XML。但是,当我尝试使用&#39; @XmlRootElement(name =&#34; user&#34;)&#39;我收到此错误:
Exception in thread "main" java.lang.RuntimeException: Unable to parse XML:
at com.example.findAccount(AccountService.java:30)
at com.example.main(AccountService.java:50)
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"return_data"). Expected elements are <{}user>
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)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at com.example.findAccount(AccountService.java:26)
... 1 more
这是我的帐户模型类:
package com.example;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class Account {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
服务:
package com.example;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.example.Account;
public class AccountService {
public Optional<Account> findAccount(final String username) {
try {
URL url = new URL("https://example.com/foo/bar" + username);
JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Account account = (Account) jaxbUnmarshaller.unmarshal(url);
return Optional.ofNullable(account);
} catch (JAXBException | MalformedURLException e) {
throw new RuntimeException("Unable to parse XML:", e);
}
}
public static void main(String[] args) {
// test it //
new AccountService().findAccount("user");
}
}
答案 0 :(得分:0)
JAXB unmarshalls完整的XML,@XmlRootElement(name = "user")
不会过滤&#34;您在XML&#34;中看到的第一个user
元素。
如果你想要这个(&#34;你在XML&#34;中看到的第一个user
元素,那么有几个选项。
user
对象。user
节点并解组此节点。如果您在user
元素周围没有太多XML,我只需解析完整的XML。否则我选择使用StAX选项。这将是一个问题:
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("https://example.com/foo/bar" + username);
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();
while(!xsr.getLocalName().equals("user")) {
xsr.nextTag();
}
JAXBContext jc = JAXBContext.newInstance(Account.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Account> jb = unmarshaller.unmarshal(xsr, Account.class);
xsr.close();
Account user = jb.getValue();
(通过一些调整从this article复制粘贴,未进行任何测试。)