使用 Xstream 解析 XML 以获取架构位置

发布于 2024-11-24 17:14:51 字数 462 浏览 1 评论 0原文

我无法使用 xstream 从 XML 中查找架构位置。

<order xmlns="http://www.mycompany.com/xml/myproject"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="test.xsd">

为了通过模式验证 XML,我使用了 javax :

Validator validator = schema.newValidator();
validator.validate(source);

现在我已将模式名称硬编码为“test.xsd”,但我希望这只是一个临时修复。

I'm having trouble finding out the schema location from the XML using the xstream.

<order xmlns="http://www.mycompany.com/xml/myproject"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="test.xsd">

For validation of the XML, with the schema, I'm using the javax :

Validator validator = schema.newValidator();
validator.validate(source);

For now I've hardcoded the schema name as "test.xsd", but I hope that is just a temporary fix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-12-01 17:14:52

默认情况下,XStream 不支持命名空间,但我认为您可以启用它。您应该能够在网站上找到详细信息。不过,为了访问命名空间,您可以像对待任何其他属性一样对待它:

public static void main(String[] args) {
    String xml = "<x:foo xmlns:x=\"http://foo.com\">" +
                         "<bar xmlns=\"http://bar.com\"/>" +
                         "</x:foo>";
    XStream xstream = new XStream();
    xstream.alias("x:foo", Foo.class);
    xstream.useAttributeFor(Foo.class, "xmlns");
    xstream.aliasField("xmlns:x", Foo.class, "xmlns");
    xstream.alias("bar", Bar.class);
    xstream.useAttributeFor(Bar.class, "xmlns");
    xstream.aliasField("xmlns", Foo.class, "xmlns");
    Object o = xstream.fromXML(xml);
    System.out.println("Unmarshalled a " + o.getClass());
    System.out.println("Value: " + o);
}

static class Foo {
    private String xmlns;
    private Bar bar;
    public String toString() {
        return "Foo{xmlns='" + xmlns + "', bar=" + bar + '}';
    }
}

static class Bar {
    private String xmlns;
    public String toString() {
        return "Bar{xmlns='" + xmlns + "'}";
    }
}

XStream isn't namespace-aware by default, though I think you can enable that. You should be able to find details on the website. To just get access to the namespace, though, you can treat it like any other attribute:

public static void main(String[] args) {
    String xml = "<x:foo xmlns:x=\"http://foo.com\">" +
                         "<bar xmlns=\"http://bar.com\"/>" +
                         "</x:foo>";
    XStream xstream = new XStream();
    xstream.alias("x:foo", Foo.class);
    xstream.useAttributeFor(Foo.class, "xmlns");
    xstream.aliasField("xmlns:x", Foo.class, "xmlns");
    xstream.alias("bar", Bar.class);
    xstream.useAttributeFor(Bar.class, "xmlns");
    xstream.aliasField("xmlns", Foo.class, "xmlns");
    Object o = xstream.fromXML(xml);
    System.out.println("Unmarshalled a " + o.getClass());
    System.out.println("Value: " + o);
}

static class Foo {
    private String xmlns;
    private Bar bar;
    public String toString() {
        return "Foo{xmlns='" + xmlns + "', bar=" + bar + '}';
    }
}

static class Bar {
    private String xmlns;
    public String toString() {
        return "Bar{xmlns='" + xmlns + "'}";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文