gpt4 book ai didi

java - JAXB 中的 XML 解析问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:26 24 4
gpt4 key购买 nike

我正在尝试从第三方 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 注释来指定 namespace 限定。包注释放置在一个名为 package-info 的特殊类中,该类必须与您的域类位于同一包中。以下是适用于您的用例的示例。

包信息

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

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

了解更多信息

关于java - JAXB 中的 XML 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10118948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com