这里我有一些代码负责获取属性值:
currPost.Body = reader.getAttributeValue("", "Body");
所以,Body
正是我的问题所基于的属性。
我的 XML 文件代表 SQL Server 转储,格式如下:
<?xml version="1.0" encoding="utf-8"?>
<posts>
<row Id="1" PostTypeId="1" AcceptedAnswerId="65" CreationDate="2011-05-24T19:28:37.853" Score="13" ViewCount="964" Body="<p>Sehr viele Märchen beginnen auf Deutsch mit "Es war einmal", aber ich kenne auch ein Märchen, das anfängt mit "Zu der Zeit, als das Wünschen noch geholfen hat ...".</p>

<p>Gibt es noch andere Beginnformeln und wenn ja, kann man diese dem geographischen Ursprung der Märchen zuordnen?</p>

<blockquote>
 <p>Many German fairy tales open with
 "Es war einmal", but some start with
 "Zu der Zeit, als das Wünschen noch
 geholfen hat ...".</p>
 
 <p>Are there any other common
 introductions? If so, is there a correlation between their use and the geographic origin of the story?</p>
</blockquote>
" OwnerUserId="4" LastEditorUserId="2" LastEditDate="2011-05-25T05:39:54.580" LastActivityDate="2011-05-25T11:56:08.270" Title="Gibt es andere übliche Märchenbeginnformeln neben "es war einmal"?" Tags="<regional>" AnswerCount="2" CommentCount="2" FavoriteCount="4" />
问题本身是,当我检索“Body”值时 - 我总是得到一个缩短的字符串,其中缺少很多字符/符号。
查看 Body
值。这是
Body="<p>Sehr viele Märchen beginnen auf Deutsch mit "Es war einmal", aber ich kenne auch ein Märchen, das anfängt mit "Zu der Zeit, als das Wünschen noch geholfen hat ...".</p>

<p>Gibt es noch andere Beginnformeln und wenn ja, kann man diese dem geographischen Ursprung der Märchen zuordnen?</p>

<blockquote>
 <p>Many German fairy tales open with
 "Es war einmal", but some start with
 "Zu der Zeit, als das Wünschen noch
 geholfen hat ...".</p>
 
 <p>Are there any other common
 introductions? If so, is there a correlation between their use and the geographic origin of the story?</p>
</blockquote>
"
所以,当我用 System.out.println()
打印时,我只得到这个
<p>Sehr viele Märchen beginnen auf Deutsch mit "Es war einmal", aber ich kenne auch ein Märchen, das anfängt mit "Zu der Zeit, als das Wünschen noch geholfen hat ...".</p>
如您所见 - Body
值包含 html 标签。这会是问题所在吗?我该怎么做?
或者也许还有其他解决方案?
非常感谢!
最佳答案
我怀疑问题出在 XML 中的
上。那是 U+000A,或“换行”(两次)。您可以通过完全删除 XML 来验证这是否是问题所在。看看它的作用:
System.out.println("Line 1\nLine2\nLine 3");
根据您的控制台,它可能会出现在多行中。这也是我希望 XML 中的字符串发生的情况。我的猜测是您只查看以属性文本开头开头的行,或者您的控制台不支持多行输出。不管怎样,上面的快速测试应该能让您了解会发生什么。
关于java - 使用 XMLStreamReader 在 Java 中解析 XML 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306828/