RDF + XML响应 - 缺少xml版本标记

时间:2013-02-15 10:13:10

标签: java xml servlets rdf

我在Java中创建了一个创建RDF / XML-ABBREV模型的sevlet。我的问题是响应不包含xml版本标记。

当前回复:

<rdf:RDF
    xmlns:myNS="http://www.sap.de/research/BusinessObjectOntology#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <myNS:Resource rdf:about="http://localhost/myResource">
    ...
  </myNS:Resource>
</rdf:RDF>

但我需要在模型开头的<?xml version="1.0"?>。 我是否必须在代码中添加内容或为什么xml版本标签不显示?

resp.setCharacterEncoding(CHARSET);
resp.setContentType("application/rdf+xml");
resp.setStatus(HttpStatus.OK_200);
model.write(resp.getWriter(), "RDF/XML-ABBREV");

感谢

1 个答案:

答案 0 :(得分:2)

声明是可选的,但您可以要求它。

根据the XML writer documentation

  

当要求写入使用UTF-8或UTF-16以外的某些编码的OutputStreamWriter时,默认行为仅提供XML声明。

要自定义输出,请尝试:

...
RDFWriter writer = model.getWriter("RDF/XML-ABBREV");
writer.setProperty("showXmlDeclaration","true");
writer.write(model, resp.getWriter(), null);