I have some xml code in a string variable that I want to pass to my JAXB
in order to parse it into a java object. However, when I run my code it doesn't seem to grab anything. It passes back an error with ClassNotFoundException
. I am not sure if it is because I setup the names in the class wrong or I just forgot I certain type of annotation in the class. I tried changing some of the names but I still receive the same error.
Error:
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at com.example.taddmsdk.Runner.meToo(Runner.java:18)
at com.example.taddmsdk.TaddmsdkApplication.run(TaddmsdkApplication.java:75)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.example.taddmsdk.TaddmsdkApplication.main(TaddmsdkApplication.java:31)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
... 11 more
XML:
<?xml version="1.0" encoding="UTF-8"?>
<results
xmlns="info"
xmlns:coll="info"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="info">
<IIsWebServiceImpl array="1"
guid="063D0DD72D5F3A46B118D7A963306361" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMWebService">
<modules array="1" guid="FBVRKJNR75858NND2UJ3J2IC9C9R" xsi:type="coll:com.collation.platform.model.topology.app.web.klm.KLMModule">
<fileName>D:\pubKSRF</fileName>
<isPlaceholder>false</isPlaceholder>
<displayName/>
<hierarchyDomain>app.web.klm</hierarchyDomain>
<hierarchyType>KLMModule</hierarchyType>
</modules>
<modules array="2" guid="IJECI89585U5FJIDNVU3FI2NF" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
<fileName>d:\SRFdata</fileName>
<isPlaceholder>false</isPlaceholder>
<displayName/>
<hierarchyDomain>app.web.klm</hierarchyDomain>
<hierarchyType>KLMModule</hierarchyType>
</modules>
<modules array="3" guid="VNJFVFV8238DNCNCJ3J4JNDIEJC875" xsi:type="coll:com.platform.model.topology.app.web.klm.KLMModule">
<fileName>D:\srfData</fileName>
<isPlaceholder>false</isPlaceholder>
<displayName/>
<hierarchyDomain>app.web.klm</hierarchyDomain>
<hierarchyType>KLMModule</hierarchyType>
</modules>
<isPlaceholder>false</isPlaceholder>
<displayName/>
<hierarchyDomain>app.web.klm</hierarchyDomain>
<hierarchyType>KLMWebService</hierarchyType>
</IIsWebServiceImpl>
</results>
Java Class:
package com.example.xmlClass;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class KLMWebServiceImpl {
ArrayList <Object> modules = new ArrayList <Object> ();
private String isPlaceholder;
private String hierarchyType;
private String hierarchyDomain;
private String displayName;
private String array;
private String guid;
private String xsiType;
//Getters
public ArrayList<Object> getModules() {
return modules;
}
public String getIsPlaceholder() {
return isPlaceholder;
}
public String getHierarchyType() {
return hierarchyType;
}
public String getHierarchyDomain() {
return hierarchyDomain;
}
public String getDisplayName() {
return displayName;
}
public String getArray() {
return array;
}
public String getGuid() {
return guid;
}
public String getXsiType() {
return xsiType;
}
//Setters
@XmlElement
public void setArray(String array) {
this.array = array;
}
@XmlElement
public void setHierarchyDomain(String hierarchyDomain) {
this.hierarchyDomain = hierarchyDomain;
}
@XmlElement
public void setGuid(String guid) {
this.guid = guid;
}
@XmlElement
public void setXsiType(String xsiType) {
this.xsiType = xsiType;
}
@XmlElement
public void setModules(ArrayList<Object> modules) {
this.modules = modules;
}
@XmlElement
public void setIsPlaceholder(String isPlaceholder) {
this.isPlaceholder = isPlaceholder;
}
@XmlElement
public void setHierarchyType(String hierarchyType) {
this.hierarchyType = hierarchyType;
}
@XmlElement
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
** Java JAXB:**
public void xmlObjVal(String xml) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(KLMWebServiceImpl.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
KLMWebServiceImpl customer = (KLMWebServiceImpl) jaxbUnmarshaller.unmarshal(new StringReader(xml));
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
}