我正在尝试针对XML文件验证JAXB架构,并且我不断收到上述错误。你能解释一下< {} Quote>手段?错误来自哪里? Quote是从XSD文件创建的。是因为“引号”元素没有定义?这是我的代码。
package khoa;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import khoa.Quote;
import khoa.Quotes;
public class EmployeeUnmarshaller {
public static void main(String[] args) throws JAXBException, SAXException {
new EmployeeUnmarshaller().runEmployeeUnmarshaller();
}
private void runEmployeeUnmarshaller() throws JAXBException, SAXException {
JAXBContext context = JAXBContext.newInstance(Quote.class);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("quotes.xsd"));
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new EmployeeValidationEventHandler());
System.out.println(unmarshaller.unmarshal(new File("quotes.xml")));
}
}
class EmployeeValidationEventHandler implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
System.out.println("\nEVENT");
System.out.println("SEVERITY: " + event.getSeverity());
System.out.println("MESSAGE: " + event.getMessage());
System.out.println("LINKED EXCEPTION: " + event.getLinkedException());
System.out.println("LOCATOR");
System.out.println(" LINE NUMBER: " + event.getLocator().getLineNumber());
System.out.println(" COLUMN NUMBER: " + event.getLocator().getColumnNumber());
System.out.println(" OFFSET: " + event.getLocator().getOffset());
System.out.println(" OBJECT: " + event.getLocator().getObject());
System.out.println(" NODE: " + event.getLocator().getNode());
System.out.println(" URL: " + event.getLocator().getURL());
return true;
}
}
这是我得到的错误:
EVENT
SEVERITY: 2
MESSAGE: unexpected element (uri:"", local:"response"). Expected elements are <{}quote>
LINKED EXCEPTION: null
LOCATOR
LINE NUMBER: 2
COLUMN NUMBER: 53
OFFSET: -1
OBJECT: null
NODE: null
URL: file:/home/khoa/Documents/work/validation-xsd/quotes.xml
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"response"). Expected elements are <{}quote>
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.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:87)
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:374)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
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:643)
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 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at khoa.EmployeeUnmarshaller.runEmployeeUnmarshaller(EmployeeUnmarshaller.java:36)
at khoa.EmployeeUnmarshaller.main(EmployeeUnmarshaller.java:22)
这是XSD文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="response" type="QuotesResponse"></xs:element>
<xs:complexType name="QuotesResponse">
<xs:sequence>
<xs:element name="elapsedtime" type="xs:int"></xs:element>
<xs:element name="quotes">
<xs:complexType>
<xs:sequence>
<xs:element name="quote" type="Quote" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="error" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string"></xs:attribute>
</xs:complexType>
<xs:complexType name="Quote">
<xs:sequence>
<xs:element name="adp_100" type="xs:double"></xs:element>
<xs:element name="adp_200" type="xs:double"></xs:element>
<xs:element name="adp_50" type="xs:double"></xs:element>
<xs:element name="adv_21" type="xs:int"></xs:element>
<xs:element name="adv_30" type="xs:int"></xs:element>
<xs:element name="adv_90" type="xs:int"></xs:element>
<xs:element name="ask" type="xs:double"></xs:element>
<xs:element name="ask_time" type="xs:string"></xs:element>
<xs:element name="asksz" type="xs:int"></xs:element>
<xs:element name="basis" type="xs:int"></xs:element>
<xs:element name="beta" type="xs:double"></xs:element>
<xs:element name="bid" type="xs:double"></xs:element>
<xs:element name="bid_time" type="xs:string"></xs:element>
<xs:element name="bidsz" type="xs:int"></xs:element>
<xs:element name="bidtick" type="xs:int"></xs:element>
<xs:element name="chg" type="xs:double"></xs:element>
<xs:element name="chg_sign" type="xs:string"></xs:element>
<xs:element name="chg_t" type="xs:double"></xs:element>
<xs:element name="cl" type="xs:double"></xs:element>
<xs:element name="cusip" type="xs:int"></xs:element>
<xs:element name="date" type="xs:date"></xs:element>
<xs:element name="datetime" type="xs:dateTime"></xs:element>
<xs:element name="div" type="xs:double"></xs:element>
<xs:element name="divexdate" type="xs:int"></xs:element>
<xs:element name="divpaydt" type="xs:int"></xs:element>
<xs:element name="dollar_value" type="xs:int"></xs:element>
<xs:element name="eps" type="xs:double"></xs:element>
<xs:element name="exch" type="xs:string"></xs:element>
<xs:element name="exch_desc" type="xs:string"></xs:element>
<xs:element name="hi" type="xs:double"></xs:element>
<xs:element name="iad" type="xs:int"></xs:element>
<xs:element name="incr_vl" type="xs:int"></xs:element>
<xs:element name="last" type="xs:double"></xs:element>
<xs:element name="lo" type="xs:double"></xs:element>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="op_flag" type="xs:int"></xs:element>
<xs:element name="opn" type="xs:double"></xs:element>
<xs:element name="pchg" type="xs:string"></xs:element>
<xs:element name="pchg_sign" type="xs:string"></xs:element>
<xs:element name="pcls" type="xs:double"></xs:element>
<xs:element name="pe" type="xs:double"></xs:element>
<xs:element name="phi" type="xs:double"></xs:element>
<xs:element name="plo" type="xs:double"></xs:element>
<xs:element name="popn" type="xs:double"></xs:element>
<xs:element name="pr_adp_100" type="xs:double"></xs:element>
<xs:element name="pr_adp_200" type="xs:double"></xs:element>
<xs:element name="pr_adp_50" type="xs:double"></xs:element>
<xs:element name="pr_date" type="xs:date"></xs:element>
<xs:element name="prbook" type="xs:double"></xs:element>
<xs:element name="prchg" type="xs:double"></xs:element>
<xs:element name="pvol" type="xs:int"></xs:element>
<xs:element name="secclass" type="xs:int"></xs:element>
<xs:element name="sesn" type="xs:string"></xs:element>
<xs:element name="sho" type="xs:string"></xs:element>
<xs:element name="symbol" type="xs:string"></xs:element>
<xs:element name="tcond" type="xs:string"></xs:element>
<xs:element name="timestamp" type="xs:int"></xs:element>
<xs:element name="tr_num" type="xs:int"></xs:element>
<xs:element name="tradetick" type="xs:string"></xs:element>
<xs:element name="trend" type="xs:string"></xs:element>
<xs:element name="vl" type="xs:int"></xs:element>
<xs:element name="volatility12" type="xs:double"></xs:element>
<xs:element name="vwap" type="xs:double"></xs:element>
<xs:element name="wk52hi" type="xs:double"></xs:element>
<xs:element name="wk52hidate" type="xs:int"></xs:element>
<xs:element name="wk52lo" type="xs:double"></xs:element>
<xs:element name="wk52lodate" type="xs:int"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
以下是XML内容:
<?xml version="1.0" encoding="UTF-8"?>
<response id="2a67cd61-7244-45f8-b39f-0bc0f5694362">
<elapsedtime>0</elapsedtime>
<quotes>
<quotetype>Delayed - market data delayed by at least 15 minutes</quotetype>
<quote>
<adp_100>31.7769</adp_100>
<adp_200>27.8477</adp_200>
<adp_50>31.3177</adp_50>
<adv_21>15638419</adv_21>
<adv_30>19404542</adv_30>
<adv_90>26872030</adv_90>
<ask>0.000</ask>
<ask_time>00:00</ask_time>
<asksz>0</asksz>
<basis>na</basis>
<beta>0.8023</beta>
<bid>0.000</bid>
<bid_time>00:00</bid_time>
<bidsz>0</bidsz>
<bidtick>d</bidtick>
<chg>0</chg>
<chg_sign>e</chg_sign>
<chg_t>na</chg_t>
<cl>33.520</cl>
<contract_size>na</contract_size>
<cusip>na</cusip>
<date>2018-05-25</date>
<datetime>2018-05-25T00:00:00-04:00</datetime>
<days_to_expiration>na</days_to_expiration>
<div>na</div>
<divexdate>na</divexdate>
<divfreq>Q</divfreq>
<divpaydt>na</divpaydt>
<dollar_value>350661442.245</dollar_value>
<eps>0.01</eps>
<exch>NYSE</exch>
<exch_desc>New York Stock Exchange</exch_desc>
<hi>33.990</hi>
<iad>na</iad>
<idelta>na</idelta>
<igamma>na</igamma>
<imp_volatility>na</imp_volatility>
<incr_vl>0</incr_vl>
<irho>na</irho>
<issue_desc>na</issue_desc>
<itheta>na</itheta>
<ivega>na</ivega>
<last>33.630</last>
<lo>33.310</lo>
<name>TWITTER INC</name>
<op_delivery>na</op_delivery>
<op_flag>1</op_flag>
<op_style>na</op_style>
<op_subclass>na</op_subclass>
<openinterest>na</openinterest>
<opn>33.540</opn>
<opt_val>na</opt_val>
<pchg>0.00</pchg>
<pchg_sign>na</pchg_sign>
<pcls>33.630</pcls>
<pe>3363</pe>
<phi>33.755</phi>
<plo>33.120</plo>
<popn>33.440</popn>
<pr_adp_100>31.8031</pr_adp_100>
<pr_adp_200>27.7884</pr_adp_200>
<pr_adp_50>31.2283</pr_adp_50>
<pr_date>2018-05-24</pr_date>
<pr_openinterest>na</pr_openinterest>
<prbook>5.019</prbook>
<prchg>0.080</prchg>
<prem_mult>na</prem_mult>
<put_call>na</put_call>
<pvol>0</pvol>
<qcond>38</qcond>
<rootsymbol>na</rootsymbol>
<secclass>0</secclass>
<sesn>na</sesn>
<sho>752667000</sho>
<strikeprice>na</strikeprice>
<symbol>TWTR</symbol>
<tcond>29</tcond>
<timestamp>1527488058</timestamp>
<tr_num>41737</tr_num>
<tradetick>e</tradetick>
<trend>na</trend>
<under_cusip>na</under_cusip>
<undersymbol>na</undersymbol>
<vl>10424410</vl>
<volatility12>0.4884</volatility12>
<vwap>33.638</vwap>
<wk52hi>36.80</wk52hi>
<wk52hidate>20180314</wk52hidate>
<wk52lo>15.67</wk52lo>
<wk52lodate>20170811</wk52lodate>
<xdate>na</xdate>
<xday>na</xday>
<xmonth>na</xmonth>
<xyear>na</xyear>
<yield>na</yield>
</quote>
<quote>
<adp_100>11.0581</adp_100>
<adp_200>11.5676</adp_200>
<adp_50>11.2900</adp_50>
<adv_21>26975556</adv_21>
<adv_30>32302687</adv_30>
<adv_90>40297310</adv_90>
<ask>0.000</ask>
<ask_time>00:00</ask_time>
<asksz>0</asksz>
<basis>na</basis>
<beta>0.8333</beta>
<bid>0.000</bid>
<bid_time>00:00</bid_time>
<bidsz>0</bidsz>
<bidtick>d</bidtick>
<chg>0</chg>
<chg_sign>e</chg_sign>
<chg_t>na</chg_t>
<cl>11.620</cl>
<contract_size>na</contract_size>
<cusip>na</cusip>
<date>2018-05-25</date>
<datetime>2018-05-25T00:00:00-04:00</datetime>
<days_to_expiration>na</days_to_expiration>
<div>0.15</div>
<divexdate>20180419</divexdate>
<divfreq>Q</divfreq>
<divpaydt>20180601</divpaydt>
<dollar_value>222009310.770</dollar_value>
<eps>19.3</eps>
<exch>NYSE</exch>
<exch_desc>New York Stock Exchange</exch_desc>
<hi>11.605</hi>
<iad>0.6</iad>
<idelta>na</idelta>
<igamma>na</igamma>
<imp_volatility>na</imp_volatility>
<incr_vl>0</incr_vl>
<irho>na</irho>
<issue_desc>na</issue_desc>
<itheta>na</itheta>
<ivega>na</ivega>
<last>11.510</last>
<lo>11.470</lo>
<name>FORD MOTOR CO. (DE)</name>
<op_delivery>na</op_delivery>
<op_flag>1</op_flag>
<op_style>na</op_style>
<op_subclass>na</op_subclass>
<openinterest>na</openinterest>
<opn>11.560</opn>
<opt_val>na</opt_val>
<pchg>0.00</pchg>
<pchg_sign>na</pchg_sign>
<pcls>11.510</pcls>
<pe>5.9637</pe>
<phi>11.650</phi>
<plo>11.440</plo>
<popn>11.450</popn>
<pr_adp_100>11.0538</pr_adp_100>
<pr_adp_200>11.5719</pr_adp_200>
<pr_adp_50>11.2869</pr_adp_50>
<pr_date>2018-05-24</pr_date>
<pr_openinterest>na</pr_openinterest>
<prbook>1.291</prbook>
<prchg>0.170</prchg>
<prem_mult>na</prem_mult>
<put_call>na</put_call>
<pvol>0</pvol>
<qcond>38</qcond>
<rootsymbol>na</rootsymbol>
<secclass>0</secclass>
<sesn>na</sesn>
<sho>3914102000</sho>
<strikeprice>na</strikeprice>
<symbol>F</symbol>
<tcond>29</tcond>
<timestamp>1527488058</timestamp>
<tr_num>39010</tr_num>
<tradetick>e</tradetick>
<trend>na</trend>
<under_cusip>na</under_cusip>
<undersymbol>na</undersymbol>
<vl>19337470</vl>
<volatility12>0.2115</volatility12>
<vwap>11.480</vwap>
<wk52hi>13.48</wk52hi>
<wk52hidate>20180116</wk52hidate>
<wk52lo>10.14</wk52lo>
<wk52lodate>20180302</wk52lodate>
<xdate>na</xdate>
<xday>na</xday>
<xmonth>na</xmonth>
<xyear>na</xyear>
<yield>5.2129</yield>
</quote>
</quotes><error>Success</error></response>
答案 0 :(得分:4)
您只能使用Quote
类创建JAXB上下文:
JAXBContext.newInstance(Quote.class);
因此,JAXB不了解配置顶级Response
元素的response
类。
其中一个选项是将Response
添加到您为JAXBContext
创建的类的枚举中:
JAXBContext.newInstance(Response.class, Quote.class)
或者,为整个包创建JAXBContext
:
JAXBContext.newInstance(Quote.class.getPackage().getName())
我更喜欢第二种选择。