我正在使用StAX和JAXB解析大型xml文件,但出现此错误:
com.ctc.wstx.exc.WstxParsingException: Received non-all-whitespace CHARACTERS or CDATA event in nextTag().
此代码为:
public class UnmarshalDemo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource(xmlpath);
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();
while(!xsr.getLocalName().equals("Name")) { //until tag is name
xsr.nextTag(); //get next tag
}
JAXBContext jc = JAXBContext.newInstance(Name.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Name> jb = unmarshaller.unmarshal(xsr, Name.class);
xsr.close();
Name name = jb.getValue();
System.out.println("First Name: "+ name.getFirstName());
System.out.println("Middle Initial: " + name.getMiddleInitial());
System.out.println("Last Name: " + name.getLastName());
}
}
对象类是:
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Name {
String FirstName;
String MiddleInitial;
String LastName;
public Name() {}
public Name(String firstName, String middleInitial, String lastName) {
super();
FirstName = firstName;
MiddleInitial = middleInitial;
LastName = lastName;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getMiddleInitial() {
return MiddleInitial;
}
public void setMiddleInitial(String middleInitial) {
MiddleInitial = middleInitial;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
@Override
public String toString() {
return "Name [FirstName=" + FirstName + ", MiddleInitial=" + MiddleInitial + ", LastName=" + LastName + "]";
}
}
xml的开头是:
<?xml version="1.0" encoding="utf-16"?>
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<FormData>
<Application>
<AppType>
<Income>1</Income>
<Option>0</Option>
收入元素是失败行。
我收到的错误是:
Exception in thread "main" com.ctc.wstx.exc.WstxParsingException: Received non-all-whitespace CHARACTERS or CDATA event in nextTag().at [row,col,system-id]: [7,38, "xmlfilepath"]
at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:621)
at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:491)
at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:475)
at com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1224)
at com.nm.ni.workflow.service.UnmarshalDemo.main(UnmarshalDemo.java:35)
我对错误的来源感到困惑,因为代码能够解析简单的示例xml,例如:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns0:findCustomerResponse xmlns:ns0="http://service.jaxws.blog/">
<InsuredData>
<Name>
<FirstName>Jane</FirstName>
<MiddleInitial>M</MiddleInitial>
<LastName>Doe</LastName>
</Name>
</InsuredData>
</ns0:findCustomerResponse>
</S:Body>
</S:Envelope>
控制台打印出:
First Name: Jane
Middle Initial: M
Last Name: Doe
,但是当它到达更复杂的xml时会导致异常。
我的问题是为什么我会收到此异常以及如何解决它?
答案 0 :(得分:0)
阅读XMLStreamReader#nextTag()的文档:
跳过任何空格(isWhiteSpace()返回true),COMMENT或 PROCESSING_INSTRUCTION,直到START_ELEMENT或END_ELEMENT为 到达。 如果不是空格字符,COMMENT, 遇到了PROCESSING_INSTRUCTION,START_ELEMENT,END_ELEMENT, 会引发异常。处理时应使用此方法 纯元素内容,用空格分隔。
我认为您应该做什么很明显:在循环测试中结合使用next()
和isStartElement()
。