使用时元帅异常 jaxb
我有一个代码 java, 而且我正在尝试拆卸它并以文件的形式保存 XML 在我的 PC, 使用 JAXB, 但我有一个例外编组:
代码 JAXB, 我使用了 :
我使用的主类:
班级 XmlConf :
请注意所有方法 /XmlSource class/ 正确执行。
</string,></string,></string></string,></string></string></string></string>
Exception in thread "main" javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write/MarshallerImpl.java:311/
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal/MarshallerImpl.java:236/
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal/AbstractMarshallerImpl.java:103/
at xml.ConfList.addToList/ConfList.java:29/
at xml.Tester.work/Tester.java:34/
at xml.Tester.main/Tester.java:16/
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError/XMLSerializer.java:237/
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot/LeafBeanInfoImpl.java:126/
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot/XMLSerializer.java:483/
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write/MarshallerImpl.java:308/
... 5 more
Java Result: 1
BUILD SUCCESSFUL /total time: 9 seconds/
代码 JAXB, 我使用了 :
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ConfList {
private static final String fileName = "Source.xml";
List<string> xmlConfList;
private Object object;
public ConfList/Object object/{
this.object = object;
}
public void addToList// throws IOException, JAXBException {
File file = new File/fileName/;
JAXBContext jaxbContext = JAXBContext.newInstance/XmlConf.class/;
Marshaller jaxbMarshaller = jaxbContext.createMarshaller//;
jaxbMarshaller.setProperty/Marshaller.JAXB_FORMATTED_OUTPUT, true/;
jaxbMarshaller.marshal/object, file/; // i got the exception here
jaxbMarshaller.marshal/object, System.out/;
}
}
我使用的主类:
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
public class Tester{
public static void main /String [] args/ throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, TransformerException, JAXBException{
work//;
}
public static void work // throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException, JAXBException{
String surl = "[url=http://api.worldweatheronline.com/free/v1/weather.ashx?q=Cairo&format=xml&num_of_days=1&key=wd63kxr294rcgvbynhaf2z4r";]http://api.worldweatheronline. ... 3B%3B[/url]
XmlSource xml = new XmlSource/surl/;
xml.validate//;
xml.load//;
ArrayList<string> paths = xml.getAllPaths//;
for/String path : paths/{
System.out.println/path/;
}
ConfList v = new ConfList/xml.getXml///;
v.addToList//;
}
}
班级 XmlConf :
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement/name = "XmlSource"/
public class XmlConf {
private URL url;
private List<string> path = new ArrayList<string>//;
private String urlp;
private Map<string, string=""> parameters;
private String host;
public URL getUrl// {
return url;
}
@XmlAttribute/name = "URL"/
public void setUrl/URL url/ {
this.url = url;
}
@XmlElement
public List<string> getPath// {
return path;
}
public void setPath/String path/ {
this.path.add/path/;
}
@XmlElement
public void setUrlPath/String urlp/{
this.urlp = urlp;
}
public String getUrlPath//{
return urlp;
}
public void setParameters/Map<string, string=""> parameters/{
this.parameters = parameters;
}
public Map<string, string=""> getParameters//{
return parameters;
}
public void setHostName/String host/{
this.host = host;
}
public String getHostName//{
return host;
}
}
请注意所有方法 /XmlSource class/ 正确执行。
</string,></string,></string></string,></string></string></string></string>
没有找到相关结果
已邀请:
1 个回复
冰洋
赞同来自:
, 然后马克斯他回到了 XML.
示范
input.xml/Output