I want to try a test about unmarshalling by xml. So I writing codes which are in a book. Codes are like this..
我想尝试一下xml的解组测试。所以我写了一本书中的代码。代码是这样的..
sqlmap.xml
sqlmap.xml
<?xml version="1.0" encoding="UTF-8"?>
<sqlmap xmlns="http://www.example.org/sqlmap/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.example.org/sqlmap/ http://www.example.org/sqlmap/sqlmap.xsd">
<sql key="add">insert</sql>
<sql key="get">select</sql>
<sql key="delete">delete</sql>
</sqlmap>
JaxbTest.java
JaxbTest.java
package springbook.learningtest.jdk.jaxb;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.junit.Test;
import springbook.user.sqlservice.jaxb.SqlType;
import springbook.user.sqlservice.jaxb.Sqlmap;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class JaxbTest {
@Test
public void readSqlmap() throws JAXBException, IOException{
String contextPath = Sqlmap.class.getPackage().getName();
JAXBContext context = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = context.createUnmarshaller();
Sqlmap sqlmap = (Sqlmap) unmarshaller.unmarshal(getClass().getResourceAsStream("sqlmap.xml"));
List<SqlType> sqlList = sqlmap.getSql();
assertThat(sqlList.size(), is(3));
assertThat(sqlList.get(0).getKey(), is("add"));
assertThat(sqlList.get(0).getValue(), is("insert"));
assertThat(sqlList.get(1).getKey(), is("get"));
assertThat(sqlList.get(1).getValue(), is("select"));
assertThat(sqlList.get(2).getKey(), is("delete"));
assertThat(sqlList.get(2).getValue(), is("delete"));
}
}
SqlMap.java
SqlMap.java
package springbook.user.sqlservice.jaxb;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"sql"
})
@XmlRootElement(name = "sqlmap")
public class Sqlmap {
@XmlElement(required = true)
protected List<SqlType> sql;
public List<SqlType> getSql() {
if (sql == null) {
sql = new ArrayList<SqlType>();
}
return this.sql;
}
}
SqlType.java
SqlType.java
package springbook.user.sqlservice.jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sqlType", propOrder = {
"value"
})
public class SqlType {
@XmlValue
protected String value;
@XmlAttribute(name = "key", required = true)
protected String key;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void setKey(String value) {
this.key = value;
}
}
But I faced with the problem because Namespace Site(http://example.org) is no more exists.. So compile is unavailable. Error code is like this..
但我遇到了这个问题,因为Namespace Site(http://example.org)不再存在。所以编译不可用。错误代码是这样的..
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.example.org/sqlmap/", local:"sqlmap"). Expected elements are <{http://www.example.org/sqlmap}sqlmap>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at springbook.learningtest.jdk.jaxb.JaxbTest.readSqlmap(JaxbTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I don't have knowledge about XML. I just want to compile it.. What I have to do?
我不了解XML。我只是想编译它..我该怎么办?
3 个解决方案
#1
0
Your elements in the XML don't match their corresponding Java classes. Your XML declares them as belonging to the XML namespace
XML中的元素与其对应的Java类不匹配。您的XML将它们声明为属于XML命名空间
xmlns="http://www.example.org/sqlmap/"
but your classes say nothing of the sort.
但你的课程没有说明这种情况。
You'll have to specify the namespace
您必须指定命名空间
XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "sql" })
@XmlRootElement(name = "sqlmap", namespace = "http://www.example.org/sqlmap/")
public class Sqlmap {
@XmlElement(namespace = "http://www.example.org/sqlmap/")
private List<SqlType> sql;
public List<SqlType> getSql() {
return sql;
}
public void setSql(List<SqlType> sql) {
this.sql = sql;
}
}
for both the root and nested elements (they both belong to the namespace).
对于根元素和嵌套元素(它们都属于命名空间)。
#2
0
xsi:schemaLocation="http://www.example.org/sqlmap/ ../../../../../sqlmap.xsd ">
| |
| |
here and here
there are some space.
有一些空间。
#3
0
An XML namespace is generally structured as a URL (it is not required to be), but it does not need to correspond to a real location. example.org is a domain that's reserved for illustrative purposes.
XML命名空间通常被构造为URL(不是必需的),但它不需要对应于真实位置。 example.org是一个保留用于说明目的的域。
Your XML document has a /
at the end of the namespace but your metadata does not. You can fix this by changing your document to the following:
您的XML文档在命名空间的末尾有/,但您的元数据没有。您可以通过将文档更改为以下内容来解决此问题:
<?xml version="1.0" encoding="UTF-8"?>
<sqlmap xmlns="http://www.example.org/sqlmap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.example.org/sqlmap/ http://www.example.org/sqlmap/sqlmap.xsd">
<sql key="add">insert</sql>
<sql key="get">select</sql>
<sql key="delete">delete</sql>
</sqlmap>
Or the namespace on the @XmlSchema
on the package-info
class.
或者package-info类上的@XmlSchema上的命名空间。
#1
0
Your elements in the XML don't match their corresponding Java classes. Your XML declares them as belonging to the XML namespace
XML中的元素与其对应的Java类不匹配。您的XML将它们声明为属于XML命名空间
xmlns="http://www.example.org/sqlmap/"
but your classes say nothing of the sort.
但你的课程没有说明这种情况。
You'll have to specify the namespace
您必须指定命名空间
XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "sql" })
@XmlRootElement(name = "sqlmap", namespace = "http://www.example.org/sqlmap/")
public class Sqlmap {
@XmlElement(namespace = "http://www.example.org/sqlmap/")
private List<SqlType> sql;
public List<SqlType> getSql() {
return sql;
}
public void setSql(List<SqlType> sql) {
this.sql = sql;
}
}
for both the root and nested elements (they both belong to the namespace).
对于根元素和嵌套元素(它们都属于命名空间)。
#2
0
xsi:schemaLocation="http://www.example.org/sqlmap/ ../../../../../sqlmap.xsd ">
| |
| |
here and here
there are some space.
有一些空间。
#3
0
An XML namespace is generally structured as a URL (it is not required to be), but it does not need to correspond to a real location. example.org is a domain that's reserved for illustrative purposes.
XML命名空间通常被构造为URL(不是必需的),但它不需要对应于真实位置。 example.org是一个保留用于说明目的的域。
Your XML document has a /
at the end of the namespace but your metadata does not. You can fix this by changing your document to the following:
您的XML文档在命名空间的末尾有/,但您的元数据没有。您可以通过将文档更改为以下内容来解决此问题:
<?xml version="1.0" encoding="UTF-8"?>
<sqlmap xmlns="http://www.example.org/sqlmap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.example.org/sqlmap/ http://www.example.org/sqlmap/sqlmap.xsd">
<sql key="add">insert</sql>
<sql key="get">select</sql>
<sql key="delete">delete</sql>
</sqlmap>
Or the namespace on the @XmlSchema
on the package-info
class.
或者package-info类上的@XmlSchema上的命名空间。