在java中将xml:base添加到xml文件中
我想在java中的xml文件中添加一个xml:base声明。 我目前在由某些第三方代码生成的 OutputStream 中拥有 xml 输出。
文件的开头是这样的:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://www.mycompany.com/myNS#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
我希望它看起来像这样:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://www.mycompany.com/myNS#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.mycompany.com/myNS">
我一定是脑子里放屁什么的,因为我想不出一个实用的好方法来做到这一点。
有任何想法吗?
I want to add an xml:base declaration to an xml file in java. I currently have the xml output in an OutputStream that was generated by some third party code.
The file starts out like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://www.mycompany.com/myNS#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
And I want it to look like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://www.mycompany.com/myNS#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.mycompany.com/myNS">
I must be having a brain fart or something, because I can't think of a good way to do this pragmatically.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过获取适当的 RDFWriter 并将其
xmlbase
属性设置为您选择的xmlbase
来更改 RDF/XML 序列化中使用的xml:base
。 以下代码从字符串中读取模型(这个问题的重要部分是关于如何编写模型,而不是它来自哪里),然后将其写入 RDF/XML 两次,每次使用不同的xml:基地
。输出是(请注意,在第一种情况下,基础是
htttp://example.org/
,在第二种情况下,它是http://example.com
(区别在于 .org 与 .com):You can change the
xml:base
used in RDF/XML serialization by obtaining the appropriate RDFWriter and setting itsxmlbase
property to your chosenxmlbase
. The following code reads a model from a string (the important part of this question is about how to write the model, not where it comes frm) and then writes it in RDF/XML twice, each time with a differentxml:base
.The output is (notice that in the first case, the base is
htttp://example.org/
and in the second, it'shttp://example.com
(the difference is .org vs. .com):ByteArrayInputStream 无法针对大文件进行扩展,而且我不喜欢使用临时文件的想法。 我还认为仅仅为了添加
xml:base
标签而将整个文件加载到 DOM 中是多余的。这是使用管道和简单的手动解析代码来添加标签的替代解决方案。
这是写入输出管道的线程代码。
The ByteArrayInputStream won't scale for large files, and I didn't like the idea of using a temp file. I also thought it was overkill to load the whole file into the DOM just to add the
xml:base
tag.Here's an alternate solution using pipes and a simple hand rolled parsing code to add the tag.
Here's the threaded code to write to the output pipe.
经过一番挖掘,这就是我所做的。
注意:我让第三方应用程序将 xml 写入 StringWriter,而不是名为“writer”的输出流。 “outputStream”是将生成的 XML 写入的流的名称。
我真的以为这会更容易。
After some digging, this is what I did.
NOTE: I had the third party app write the xml to a StringWriter instead of an output stream named 'writer'. 'outputStream' is the name of the stream the resulting XML will be written to.
I really thought this would be easier.