java org.xml.sax.SAXParseException:在序言中不允许引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3311781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.xml.sax.SAXParseException: Reference is not allowed in prolog
提问by sony
I am trying to escape html characters of a string and use this string to build a DOM XML using parseXml method shown below. Next, I am trying to insert this DOM document into database. But, when I do that I am getting the following error:
我正在尝试转义字符串的 html 字符,并使用该字符串使用如下所示的 parseXml 方法构建 DOM XML。接下来,我试图将这个 DOM 文档插入到数据库中。但是,当我这样做时,我收到以下错误:
org.xml.sax.SAXParseException: Reference is not allowed in prolog.
org.xml.sax.SAXParseException:在序言中不允许引用。
I have three questions: 1) I am not sure how to escape double quotes. I tried replaceAll("\"", """) and am not sure if this is right.
我有三个问题:1)我不知道如何转义双引号。我试过 replaceAll("\"", """) 并不确定这是否正确。
2) Suppose I want a string starting and ending with double quotes (eg: "sony"), how do I code it? I tried something like:
2)假设我想要一个以双引号开头和结尾的字符串(例如:“sony”),我该如何编码?我试过类似的东西:
String sony = "\"sony\""
字符串索尼 = "\"索尼\""
Is this right? Will the above string contain "sony" along with double quotes or is there another way of doing it?
这是正确的吗?上面的字符串会包含“sony”和双引号还是有另一种方法?
3)I am not sure what the "org.xml.sax.SAXParseException: Reference is not allowed in prolog." error means. Can someone help me fix this?
3)我不确定“org.xml.sax.SAXParseException:在序言中不允许引用”是什么。错误的意思。有人可以帮我解决这个问题吗?
Thanks, Sony
谢谢,索尼
Steps in my code:
我的代码中的步骤:
Utils. java
public static String escapeHtmlEntities(String s) { return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """). replaceAll(":", ":").replaceAll("/", "/"); }
public static Document parseXml (String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); doc.setXmlStandalone(false); return doc; }TreeController.java
protected void notifyNewEntryCreated(String entryType) throws Exception { for (Listener l : treeControlListeners) l.newEntryCreated();
final DomNodeTreeModel domModel = (DomNodeTreeModel) getModel(); Element parent_item = getSelectedEntry(); String xml = Utils.escapeHtmlEntities("<entry xmlns=" + "\"http://www.w3.org/2005/atom\"" + "xmlns:libx=" + "\"http://libx.org/xml/libx2\">" + "<title>" + "New" + entryType + "</title>" + "<updated>2010-71-22T11:08:43z</updated>" + "<author> <name>LibX Team</name>" + "<uri>http://libx.org</uri>" + "<email>[email protected]</email></author>" + "<libx:" + entryType + "></libx:" + entryType + ">" + "</entry>"); xmlModel.insertNewEntry(xml, getSelectedId());}
XMLDataModel.java
实用程序。爪哇
public static String escapeHtmlEntities(String s) { return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\" "、""")。replaceAll(":", ":").replaceAll("/", "/"); }
public static Document parseXml (String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); doc.setXmlStandalone(false); return doc; }树控制器.java
protected void notifyNewEntryCreated(String entryType) 抛出异常 { for (Listener l : treeControlListeners) l.newEntryCreated();
final DomNodeTreeModel domModel = (DomNodeTreeModel) getModel(); Element parent_item = getSelectedEntry(); String xml = Utils.escapeHtmlEntities("<entry xmlns=" + "\"http://www.w3.org/2005/atom\"" + "xmlns:libx=" + "\"http://libx.org/xml/libx2\">" + "<title>" + "New" + entryType + "</title>" + "<updated>2010-71-22T11:08:43z</updated>" + "<author> <name>LibX Team</name>" + "<uri>http://libx.org</uri>" + "<email>[email protected]</email></author>" + "<libx:" + entryType + "></libx:" + entryType + ">" + "</entry>"); xmlModel.insertNewEntry(xml, getSelectedId());}
XML数据模型.java
public void insertNewEntry (String xml, String parent_id) throws Exception { insertNewEntry(Utils.parseXml(xml).getDocumentElement(), parent_id); }
public void insertNewEntry (String xml, String parent_id) 抛出异常 { insertNewEntry(Utils.parseXml(xml).getDocumentElement(), parent_id); }
public void insertNewEntry (Element elem, String parent_id) throws Exception {
// inserting an entry with no libx: tag will create a storage leak
if (elem.getElementsByTagName("libx:package").getLength() +
elem.getElementsByTagName("libx:libapp").getLength() +
elem.getElementsByTagName("libx:module").getLength() < 1) {
// TODO: throw exception here instead of return
return;
}
XQPreparedExpression xqp = Q.get("insert_new_entry.xq");
xqp.bindNode(new QName("entry"), elem.getOwnerDocument(), null);
xqp.bindString(new QName("parent_id"), parent_id, null);
xqp.executeQuery();
xqp.close();
updateRoots();
}
- insert_new_entry.xq
- insert_new_entry.xq
declare namespace libx='http://libx.org/xml/libx2'; declare namespace atom='http://www.w3.org/2005/atom'; declare variable $entry as xs:anyAtomicType external; declare variable $parent_id as xs:string external; declare variable $feed as xs:anyAtomicType := doc('libx2_feed')/atom:feed; declare variable $metadata as xs:anyAtomicType := doc('libx2_meta')/metadata; let $curid := $metadata/curid return replace value of node $curid with data($curid) + 1, let $newid := data($metadata/curid) + 1 return insert node {$newid}{ $entry//} into $feed, let $newid := data($metadata/curid) + 1 return if ($parent_id = 'root') then () else insert node http://libx.org/xml/libx2' /> into $feed/atom:entry[atom:id=$parent_id]//(libx:module|libx:libapp|libx:package)
声明命名空间 libx=' http://libx.org/xml/libx2'; 声明命名空间原子= http://www.w3.org/2005/atom'; 将变量 $entry 声明为 xs:anyAtomicType 外部;将变量 $parent_id 声明为 xs:string external;将变量 $feed 声明为 xs:anyAtomicType := doc('libx2_feed')/atom:feed; 将变量 $metadata 声明为 xs:anyAtomicType := doc('libx2_meta')/metadata; 让 $curid := $metadata/curid 返回用 data($curid) + 1 替换节点 $curid 的值,让 $newid := data($metadata/curid) + 1 返回插入节点 {$newid}{ $entry/ /} 进入 $feed, let $newid := data($metadata/curid) + 1 return if ($parent_id = 'root') then () else 将节点 http://libx.org/xml/libx2' /> 插入$feed/atom:entry[atom:id=$parent_id]//(libx:module|libx:libapp|libx:package)
回答by mdma
To escape a double quote, use the "entity, which is predefined in XML.
要转义双引号,请使用"在 XML 中预定义的实体。
So, your example string, say an attribute value, will look like
所以,你的示例字符串,比如一个属性值,看起来像
<person name=""sony""/>
There is also 'for apostrophe/single quote.
还有'撇号/单引号。
I see you have lots of replaceAll calls, but the replacements seem to be the same? There are some other characters that cannot be used literally, but should be escaped:
我看到你有很多 replaceAll 调用,但替换似乎是一样的?还有一些其他字符不能按字面使用,但应该转义:
& --> &
> --> >
< --> <
" --> "
' --> '
(EDIT: ok, I see this is just formatting - the entities are being turned into they're actual values when being presented by SO.)
(编辑:好的,我看到这只是格式设置 - 实体在被 SO 呈现时被转换为它们的实际值。)
The SAX exception is the parser grumbling because of the invalid XML.
SAX 异常是解析器因为无效的 XML 而抱怨。
As well as escaping the text, you will need to ensure it adheres to the well-formedness rulesof XML. There's quite a bit to get right, so it's often simpler to use a 3rd party library to write out the XML. For example, the XMLWriter in dom4j.
除了转义文本外,您还需要确保它遵守XML的格式良好的规则。有很多事情要做,所以使用 3rd 方库来写出 XML 通常更简单。例如,dom4j 中的 XMLWriter 。
回答by kadalamittai
You can check out Tidy specification. its a spec released by w3c. Almost all recent languages have their own implementation.
您可以查看 Tidy 规范。它是由 w3c 发布的规范。几乎所有最近的语言都有自己的实现。
rather than just replace or care only to < ,>, & just configure JTidy ( for java ) options and parse. this abstracts all the complication of Xml escape thing.
而不是仅仅替换或只关心 < ,>,而只是配置 JTidy(对于 java )选项和解析。这抽象了 Xml 转义的所有复杂性。
i have used both python , java and marklogic based tidy implementations. all solved my purposes
我已经使用了基于 python、java 和 marklogic 的 tidy 实现。都解决了我的目的

