有没有办法使用java SimpleXML库定义SchemaLocation?
我在此处遵循API规范,手动尝试将其添加为http://simple.sourceforge.net/download/stream/doc/javadoc/
中的命名空间使用:
@NamespaceList({
@Namespace(reference="http://hello/stock", prefix="stk"),
@Namespace(reference="http://hello/basket", prefix="bsk"),
@Namespace(reference="http://hello/location", prefix="loc"),
@Namespace(reference="http://hello/common", prefix="cmn"),
@Namespace(reference="schemaLocation:http://hello/stock stock-v1.xsd", prefix="xsi"),
})
public class Response{
//
}
但每当我尝试解析将转换为此POJO类的XML文件时,它说它无法找到SchemaLocation?
以下错误:
org.simpleframework.xml.core.AttributeException: Attribute 'schemaLocation' does not have a match in class com.hello.model.Response at line 1
我想解析的xml是这样的:
<Stock
xmlns:stk="hello/stock"
xmlns:bsk="hello/basket"
xmlns:loc="hello/location"
xmlns:cmn="hello/common"
xsi:schemaLocation="hello/stock stock-v1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
uri="http://hello/stock/"
version="1"
id=""
brand="ford">
..xml data here
</Stock>
由于
答案 0 :(得分:1)
正如亚历克斯所提到的......将其移至属性。这是一个工作样本:
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
/**
*
* @author Maher
*/
@Root(name = "stock")
public class Stock {
@Attribute(name = "schemaLocation")
@Namespace(reference = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi")
private String mSchemaLocation;
@Element(name = "child")
private String mChild;
public Stock() {
setSchemaLocation("urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd");
setChild("Hi from Apipas!");
}
private void setSchemaLocation(String schemaLocation) {
this.mSchemaLocation = schemaLocation;
}
public void setChild(String child) {
this.mChild = child;
}
}
输出:
<stock xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<child>Hi from Apipas!</child>
</stock>
祝你好运,'。
答案 1 :(得分:0)
对于SimpleXML,它基本上不再是名称空间,并被视为属性。请参阅完整的回复here。
我希望这有帮助!