我收到一个像
这样的XML字符串<a:abc>
<a:xyz>
<a:a>00185550952</a:a>
<a:b>MO238620289621365901</a:b>
<a:c>*17927085</a:c>
<a:d>awb.20161020121803_3060</a:d>
<a:e>df64e8ec-d5)7-469)-))71-bbe922f)4f9</a:e>
</a:xyz>
</a:abc>
它没有任何命名空间或头信息。我以这种方式接收大约1000万字符串。解析xml字符串的有效方法。我尝试了XPATH但是它要慢。可以将jaxb解组为jaxb对象有效地
尝试过JAXB
public static void main(String[] args) throws ParserConfigurationException, JAXBException, SAXException, IOException, XMLStreamException {
String sng = "<a:abc><a:xyz><a:a>00185550952</a:a><a:b>MO238620289621365901</a:b><a:c>*17927085</a:c><a:d>awb.20161020121803_3060</a:d><a:e>df64e8ec-d5)7-469)-))71-bbe922f)4f9</a:e></a:xyz></a:abc";
InputStream inputStream = new ByteArrayInputStream(sng.getBytes(StandardCharsets.UTF_8));
JAXBContext jaxbContext = JAXBContext.newInstance(Abc.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.unmarshal(inputStream);
}
Abc.Java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "abc")
public class Abc {
@XmlElement(name = "xyz", required = true)
public Xyz xyz;
public Xyz getXyz() {
return xyz;
}
public void setXyz(Xyz xyz) {
this.xyz = xyz;
}
}**
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "xyz")
public class Xyz {
@XmlElement(name = "a")
public String a;
@XmlElement(name = "b")
public String b;
@XmlElement(name = "c")
public String c;
@XmlElement(name = "d")
public String d;
@XmlElement(name = "e")
public String e;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String getE() {
return e;
}
public void setE(String e) {
this.e = e;
}
}
异常
线程“main”中的异常javax.xml.bind.UnmarshalException - 链接异常: [org.xml.sax.SAXParseException; lineNumber:1; columnNumber:8;元素“a:abc”的前缀“a”未绑定。] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:578) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:264) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知来源) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知来源) 在 引起:org.xml.sax.SAXParseException; lineNumber:1; columnNumber:8;元素“a:abc”的前缀“a”未绑定。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258) ......还有4个