使用 XOM 编写 GraphML?

发布于 2024-08-11 18:59:58 字数 779 浏览 11 评论 0原文

我正在尝试用 java 中的 XOM 编写 graphML 文档,但我不知道如何使所有名称空间声明正确。为了拥有有效的 graphML,我需要有一个如下所示的根元素:

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
     http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

我已经能够通过执行以下操作来获得大部分内容

Element root = new Element("graphml");
root.setNamespaceURI("http://graphml.graphdrawing.org/xmlns");
root.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");

问题是该标签的最后一个元素,xsi:schemaLocation。我不知道如何在 XOM 中表达这一点。我不能将其作为普通属性来执行,因为这会引发异常(必须声明属性前缀。),并且将其作为附加命名空间声明也会导致异常(NCNames 不能包含冒号)。有什么想法吗?

I'm trying to write out a graphML document with XOM in java, but I can't figure out how to get all of the namespace declarations correct. To have valid graphML, I need to have a root element that looks like the following:

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
     http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

I've been able to get most of this by doing

Element root = new Element("graphml");
root.setNamespaceURI("http://graphml.graphdrawing.org/xmlns");
root.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");

The problem is the last element of this tag, the xsi:schemaLocation. I can't figure out how to express this in XOM. I can't do it as a normal attribute, as that throws an exception(Attribute prefixes must be declared.) and doing it as an additional namespace declaration also results in an exception(NCNames cannot contain colons). Any ideas?

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

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

发布评论

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

评论(1

你好,陌生人 2024-08-18 18:59:58

这应该可以做到。基本上,您没有为 xsi:schemaLocation 属性提供命名空间 URI。因此,尝试创建一个没有命名空间的前缀属性显然是行不通的。

root.addAttribute(new Attribute("xsi:schemaLocation",
    "http://www.w3.org/2001/XMLSchema-instance",
    "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"));

在此处检查正确的属性构造函数

属性(字符串名称、字符串 URI、字符串值)

This should do it. Basically you didn't provide the namespace URI for xsi:schemaLocation attribute. Thus trying to create a prefixed attribute with no namespace which clearly won't work.

root.addAttribute(new Attribute("xsi:schemaLocation",
    "http://www.w3.org/2001/XMLSchema-instance",
    "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"));

Check here for the correct Attribute constructor

Attribute(String name, String URI, String value)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文