gpt4 book ai didi

java - 在 java SimpleXML lib 中定义 schemaLocation?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:33 24 4
gpt4 key购买 nike

有没有办法使用 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>

谢谢

最佳答案

正如亚历克斯提到的...将其移至属性。这是一个工作示例:

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>

祝你好运,”。

关于java - 在 java SimpleXML lib 中定义 schemaLocation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24870681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com