编程语言
首页 > 编程语言> > Java-JAXB中的Xml解析问题

Java-JAXB中的Xml解析问题

作者:互联网

我正在尝试从第三方URL解析xml流.在我的控制台中看起来像.

`

 <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
      <string>CMSDocs</string>
      <string>CRMIncomingDocs</string>
      <string>CRMOutgoingDocs</string>
      <string>Customized Reports</string>
      <string>scripts</string>
      <string>Site Assets</string>
      <string>Site Pages</string>
      <string>Style Library</string>
    </ArrayOfString> 

`

而且我得到错误之后的错误.

Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
    at com.cms.webservice.converter.BaseMarshaller.convertToObject(BaseMarshaller.java:41)
    at com.cms.webservice.converter.BaseMarshaller.main(BaseMarshaller.java:90)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1058)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    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:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)
    ... 5 more
Java Result: 1

但是它正在使用XML

`

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ArrayOfString>
    <string>Kshitij-1</string>
    <string>Kshitij-2</string>
    <string>Kshitij-3</string>
    <string>Kshitij-4</string>
    <string>Kshitij-5</string>
</ArrayOfString>

`

谢谢

解决方法:

您可以使用@XmlSchema批注指定名称空间限定.程序包批注放置在一个名为package-info的特殊类上,该类必须与您的域类位于同一程序包中.下面是一个适用于您的用例的示例.

包装信息

@XmlSchema( 
    namespace = "http://tempuri.org/", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

欲获得更多信息

> http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

标签:jaxb,java
来源: https://codeday.me/bug/20191201/2081166.html