我在编组对象时遇到了这个异常:javax.xml.bind.JAXBException:class Subscription或其任何超类对此上下文都是已知的。
我知道@XmlSeeAlso有一些解决方案,并且可以修改jaxb类,但是当我们从XSD / WSDL文件生成JAXB类时,我们无法更改它们。因此,这些解决方案不适用于这种情况。
public static String getStringFromSubscription(Subscription subscription) throws MbException
{
Marshaller marshaller;
StringWriter stringWriter = new StringWriter();
try
{
marshaller = JAXBContext.newInstance(com.myhealth.com.ObjectFactory.class
.getPackage().getName()).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal(subscription, stringWriter);
}
catch (Exception e)
{
throw new MbException(e);
}
return stringWriter;
}
答案 0 :(得分:1)
您需要在import numpy as np
import csv
import lorem
import time
from multiprocessing import Pool, Value
# data
high=2**10;
nsamples=2**18;
labels=np.random.randint(0,high,nsamples)
filename='four_cores_parallel.txt'
# supposedly intensive computation
def worker(ell):
total=0
for k in range(ell):
total=total+k;
return(total)
multicore_start_time=time.time()
pool=Pool(4);
with open(filename,'w') as file:
for total in pool.map(worker, labels):
w = csv.writer(file) ;
w.writerow([total])
w.writerow(['****'])
w.writerow(lorem.paragraph())
multicore_time=time.time()-multicore_start_time;
print('Multicore write takes '+ str(multicore_time)+' seconds')
# Multicore write takes 20.171985149383545 seconds
实例中指定上下文(也就是包名)名称。它将找到该软件包中的JAXBContext.newInstance
,如文档(第1页)中所述
JAXBException-如果在创建JAXBException时遇到错误 JAXBContext,例如
- 无法在包中找到ObjectFactory.class或jaxb.index
- contextPath中包含的全局元素之间的歧义
- 找不到上下文工厂提供程序属性的值
- 在同一contextPath上混合来自不同提供程序的模式派生包
ObjectFactory.class
答案 1 :(得分:0)
据我所知,这里有3种解决方案。
ObjectFactory类是在从xsd / wsdl中生成jaxb时自动创建的。
marshaller.marshal(new com.myhealth.com.ObjectFactory().createSubscription(subscription), stringWriter);
JAXBContext.newInstance(Subscription.class).createMarshaller();
JAXBContext.newInstance(com.myhealth.com.ObjectFactory.class
.getPackage().getName()).createMarshaller();