当您使用 JAXB 生成 java 类时,您将始终获得一个名为 ObjectFactory 的类。在此类中,有一个私有属性 QName,其中包含命名空间。
是否可以告诉生成器使该属性可以从外部访问。那么也许将其公开或为其创建一个吸气剂?
此类的实例是大型对象图的一部分,并且不在对象图的根部:
public class Day
{
public Day(LocalDate date, List<LocalTime> times)
{
this.date = date;
this.times = times;
}
public Day()
{
this(null, null);
}
public LocalDate getDate()
{
return date;
}
public List<LocalTime> getTimes()
{
return times;
}
private final LocalDate date;
private final List<LocalTime> times;
}
Run Code Online (Sandbox Code Playgroud)
使用 Jersey 和 JAXB 将对象图转换为 JSON。我已经XmlAdapter注册了LocalDate和LocalTime。
问题是它只对date财产有效,而不是对times财产有效。我怀疑这与列表times而不是单个值有关。那么,我如何告诉 Jersey/JAXBtimes使用注册的来编组列表中的每个元素XmlAdapter?
更新:
通过添加标量属性并观察 JSON 中的预期输出,我确认LocalTime编组确实适用于标量属性。LocalTimeLocalTime
为了完整起见,这里是 …
我在 Weblogic 10.3 上有一个正在运行的 Web 服务 (JAX-WS 2.2)。一切正常。所以wsdl中不可能有问题。
我尝试迁移到 WLS 12。但现在我在部署时出错。
从 WLS 10 到 WLS 12 的哪些变化会导致问题?
在以下代码中,我收到以下异常。XmlAttribute/XmlValue 无法正常工作,我无法识别:-
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:
at public java.util.Set nl.magnus.test.AddressComponent.getLocationTypeSet()
at nl.magnus.test.AddressComponent
Run Code Online (Sandbox Code Playgroud)
我的豆位置类型:-
@XmlRootElement(name = "type")
public class LocationType {
private Integer locationTypeId;
private String type;
private String status;
@Override
public String toString() {
return "LocationType [locationTypeId=" + locationTypeId + ", type=" + type + ", status=" + status + "]";
}
public Integer getLocationTypeId() {
return locationTypeId; …Run Code Online (Sandbox Code Playgroud) 我收到以下响应并尝试使用 JAXB 解析它,但以下代码返回以下错误。
Exception in thread "main" org.springframework.http.converter.HttpMessageConversionException: Could not instantiate JAXBContext for class [class com.easytobook.EasytobookResponse]: 1 counts of IllegalAnnotationExceptions; nested exception is com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.easytobook.Roomtype#totalTaxesAndFees has mutually exclusive annotations @javax.xml.bind.annotation.XmlAttribute and @javax.xml.bind.annotation.XmlElement
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlAttribute(name=EUR, required=false, namespace=##default)
Run Code Online (Sandbox Code Playgroud)
我知道异常与 totalTaxsesAndFees 相关,但我不确定如何解决该问题。
EasytobookResponse response = restTemplate.postForObject(
url, easy, EasytobookResponse.class);
System.err.println("RESPONSE >>>>"
+ response.getResponse().getHotelInfo().get(0).getId());
@XmlRootElement(name = "EasytobookResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class EasytobookResponse {
@XmlAttribute(name = "target")
private String target;
@XmlAttribute(name = "username")
private …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的实用程序,用于验证开发人员添加到 pom.xml 的内容。例如,可能存在限制,即所有依赖项版本必须由父 pom 管理,并且如果开发人员向其中一个子项目中的依赖项添加版本标记,则该实用程序应该捕获它。为此,我试图将 pom.xml 的内容加载到 maven 模型类“org.apache.maven.model.Model”中。但是,我收到此异常:
unexpected element (uri:"http://maven.apache.org/POM/4.0.0", local:"project"). Expected elements are (none)
Run Code Online (Sandbox Code Playgroud)
添加自定义验证处理程序以忽略“意外元素”异常(实际上,始终返回 true)不起作用。这是我的代码:
public static void main(String[] args) {
try {
unmarshalXMLToObject(new File("pom.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Model unmarshalXMLToObject(File xmlFileToUnmarshal) throws JAXBException, SAXException, IOException,
ParserConfigurationException {
JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
Unmarshaller m = jaxbContext.createUnmarshaller();
m.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent arg0) {
// return true always
return true;
}
});
@SuppressWarnings("unchecked")
Model objectToReturn = (Model) m.unmarshal(xmlFileToUnmarshal);
return …Run Code Online (Sandbox Code Playgroud) 我不断收到 JAXB 转换错误。
我不知道在这一点上要纠正什么。
解组:
try{
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter(API_KEY, ""));
WebResource webResource = client.resource("https://url.entries.xml");
webResource.setProperty("", API_KEY);
ClientResponse response = webResource.accept("application/xml").get(ClientResponse.class);
if(response.getStatus() != 200){
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("\n============getFtoCResponse============");
System.out.println(output);
JAXBContext jaxbContext = JAXBContext.newInstance(Entries.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Entries itsEntries = (Entries)((JAXBElement)unmarshaller.unmarshal(new StringReader(output))).getValue();
/* Object o = unmarshaller.unmarshal(new StringReader(output));
System.out.println(o.getClass());*/
}catch(Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Entry.java 类的前 16 行,我已经注释了 setter 和 getter:
@XmlRootElement(name = "Entries")
@XmlAccessorType(XmlAccessType.FIELD) …Run Code Online (Sandbox Code Playgroud) 嗨,我已经使用 wsimport 从 WSDL 生成了 java 类。但是我已经将响应写入文件 *.xml。但是现在我想读取这个 xml 文件并填充已经生成的 java 类。
我试过了:
JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(source);
Run Code Online (Sandbox Code Playgroud)
或者
JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(new File("file.xml"));
Run Code Online (Sandbox Code Playgroud)
Report 是我发送请求时作为响应得到的类
在第一种情况下,我得到
javax.xml.bind.UnmarshalException: unexpected element (uri: "", local:"soap:Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+
Run Code Online (Sandbox Code Playgroud)
在第二种情况下
javax.xml.bind.UnmarshalException: unexpected element (uri: "http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+
Run Code Online (Sandbox Code Playgroud)
XML 是这样的:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns3:GetReportOnlineResponse xmlns:ns2="http://pagewhereisthewsdl.com/document" xmlns:ns3="http://pagewhereisthewsdl.com/endpoint">
<ns2:Report>
...
</ns2:Report>
</ns3:GetReporteOnlineResponse>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
或者我能做什么?
哪一种是读取xml的有效方式。我知道两种方法:
1)JAXB:通过使用 jaxb 注释来注释我的类,我们使用对象的编组和解组在 java 对象中获取 xml,反之亦然。
2)DOM:使用dom解析器解析xml,使用xml中的xpath值可以访问。
DOM 示例:
File fXmlFile = new File("/Users/link1/input.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Run Code Online (Sandbox Code Playgroud)
根据业务需求,我希望在上述两者之间使用最快的方式和更好的方式。建议和很少的策略将不胜感激。
我目前正在尝试使用 docx4j 库将文件从 html 转换为 docx。
我已经设法使用 itext5 将 html 转换为 pdf,但现在由于 jaxb,我正面临尝试转换为 docx 的异常。
我的项目使用 maven,所以我试图导入很多库......徒劳......
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-ImportXHTML</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>xhtmlrenderer</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-jxc</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.jaxb-namespaceprefixmapper-interfaces</groupId>
<artifactId>JAXBNamespacePrefixMapper</artifactId> …Run Code Online (Sandbox Code Playgroud) jaxb ×10
java ×9
xml ×4
docx4j ×1
dom ×1
jakarta-ee ×1
jersey ×1
json ×1
marshalling ×1
maven ×1
spring ×1
web-services ×1
weblogic ×1
wsdl ×1
xjc ×1
xmladapter ×1
xpath ×1