使用xjc我从以下xml生成了以下java类。
xjc命令:
xjc -p sample sample.xsd
从sample.xml生成的xsd文件:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://rssnamespace.org/feedburner/ext/1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="origLink" type="xs:anyURI"/>
<xs:element name="info">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="uri"
use="optional"/>
<xs:attribute type="xs:string" name="rel"
use="optional"/>
<xs:attribute type="xs:anyURI" name="href"
use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
sample.xml:
<?xml version=\\\"1.0\" encoding=\\\"UTF-8\"\\?>
<?xml-stylesheet type="text/xsl" media="screen\" href=\"/~d/styles/rss2full.xsl\"?>
<?xml-stylesheet type="text/css" media="screen\" href=\"http://feeds.arsenal.com/~d/styles/itemcontent.css"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/\" xmlns:feedburner=\"http://rssnamespace.org/feedburner/ext/1.0" version=\"2.0\">
<channel>
<title><![CDATA[Arsenal FC News Feed]]></title>
<link>http://feeds.arsenal.com/arsenal-news</link>
<description><![CDATA[Arsenal FC News Headlines]]></description>
<pubDate>Thu, 05 Dec 2013 16:05:34 +0000</pubDate>
<managingEditor>website@arsenal.co.uk (Arsenal)</managingEditor>
<copyright>Copyright(C) 2013 Arsenal Broadband Ltd</copyright>
<generator>Arsenal</generator>
<language>en-gb</language>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.arsenal.com/arsenal-news" />
<feedburner:info uri="arsenal-news" />
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" />
<item>
<title><![CDATA[Peter Hill-Wood honoured by the Club]]></title>
<link>http://feeds.arsenal.com/~r/arsenal-news/~3/OZfGaLyOb9I/former-arsenal-chairman-honoured-by-club</link>
<guid isPermaLink="false">http://www.arsenal.com/50466</guid>
<description>Bust of former Arsenal chairman unveiled at Emirates Stadium in commemoration of his contribution to the Club<img src="http://feeds.feedburner.com/~r/arsenal-news/~4/OZfGaLyOb9I" height="1" width="1"/></description>
<pubDate>Thu, 05 Dec 2013 16:05:34 +0000</pubDate>
<feedburner:origLink>http://www.arsenal.com/news/news-archive/former-arsenal-chairman-honoured-by-club</feedburner:origLink>
</item>
</channel>
</rss>
这里是生成的java类:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2013.12.05 at 10:15:01 PM GMT
//
package rss;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="uri" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="rel" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="href" type="{http://www.w3.org/2001/XMLSchema}anyURI" />
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
@XmlRootElement(name = "info")
public class Info {
@XmlValue
protected String value;
@XmlAttribute
protected String uri;
@XmlAttribute
protected String rel;
@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String href;
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the uri property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUri() {
return uri;
}
/**
* Sets the value of the uri property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUri(String value) {
this.uri = value;
}
/**
* Gets the value of the rel property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getRel() {
return rel;
}
/**
* Sets the value of the rel property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRel(String value) {
this.rel = value;
}
/**
* Gets the value of the href property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getHref() {
return href;
}
/**
* Sets the value of the href property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setHref(String value) {
this.href = value;
}
}
我用来将xml文件转换为pojo的java代码:
public class JaxBConvert {
public static void main(String[] args) {
try {
File file = new File("C:\\sample.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Info info = (Info) jaxbUnmarshaller.unmarshal(file);
System.out.println(info);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
但是我收到了这个错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"rss"). Expected elements are <{http://rssnamespace.org/feedburner/ext/1.0}info>,<{http://rssnamespace.org/feedburner/ext/1.0}origLink>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
我的java对象是否正确?
更新:我使用http://www.freeformatter.com/xsd-generator.html生成XSD架构:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://rssnamespace.org/feedburner/ext/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="origLink" type="xs:anyURI"/>
<xs:element name="info">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="uri" use="optional"/>
<xs:attribute type="xs:string" name="rel" use="optional"/>
<xs:attribute type="xs:anyURI" name="href" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
来自这个XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.arsenal.com/~d/styles/itemcontent.css"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title><![CDATA[Arsenal FC News Feed]]></title>
<link>http://feeds.arsenal.com/arsenal-news</link>
<description><![CDATA[Arsenal FC News Headlines]]></description>
<pubDate>Thu, 05 Dec 2013 16:05:34 +0000</pubDate>
<managingEditor>website@arsenal.co.uk (Arsenal)</managingEditor>
<copyright>Copyright(C) 2013 Arsenal Broadband Ltd</copyright>
<generator>Arsenal</generator>
<language>en-gb</language>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.arsenal.com/arsenal-news" />
<feedburner:info uri="arsenal-news" />
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" />
<item>
<title><![CDATA[Peter Hill-Wood honoured by the Club]]></title>
<link>http://feeds.arsenal.com/~r/arsenal-news/~3/OZfGaLyOb9I/former-arsenal-chairman-honoured-by-club</link>
<guid isPermaLink="false">http://www.arsenal.com/50466</guid>
<description>Bust of former Arsenal chairman unveiled at Emirates Stadium in commemoration of his contribution to the Club<img src="http://feeds.feedburner.com/~r/arsenal-news/~4/OZfGaLyOb9I" height="1" width="1"/></description>
<pubDate>Thu, 05 Dec 2013 16:05:34 +0000</pubDate>
<feedburner:origLink>http://www.arsenal.com/news/news-archive/former-arsenal-chairman-honoured-by-club</feedburner:origLink>
</item>
</channel>
</rss>
更新2:
阅读此博文:http://vbvyas.wordpress.com/2011/10/11/java-parsing-rss-xml-using-xpath/ 使用xpath进行解析似乎更简单:
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
public class XmlParser {
public static void main(String[] args) {
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
URL url = new URL("http://www.somesite.com/index.php?format=feed&type=rss");
InputStream inputStream = url.openStream();
Reader reader = new InputStreamReader(inputStream, "UTF-8");
InputSource inputSource = new InputSource(reader);
Document doc = builder.parse(inputSource);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//rss/channel/item/title/text()");
NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
{
String title = nodes.item(i).getNodeValue();
System.out.println(title);
}
}
catch (Exception exception) {
exception.printStackTrace();
}
}
}
答案 0 :(得分:0)
从您生成模型的XML Schema开始,JAXB将期望文档如下所示:
<info
xmlns="http://rssnamespace.org/feedburner/ext/1.0"
url="A"
rel="B"
fref="http://www.example.com"/>
或者
<origLink>http://www.example.com</origLink>
如果要解析与sample.xml
不符合此XML模式的XML文档,则需要创建另一个映射到XML文档结构的模型。