简体   繁体   English

使用XMLStreamReader在Java中解析XML时出现问题

[英]Problems parsing XML in Java with XMLStreamReader

Here I have some code responsible for fetching the attribute value: 在这里,我有一些代码负责获取属性值:

currPost.Body = reader.getAttributeValue("", "Body");

so, Body is the very attribute my problem is based on. 因此, Body是我的问题所基于的属性。

My XML file represents SQL Server dump and is in the form as follows: 我的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="&lt;p&gt;Sehr viele Märchen beginnen auf Deutsch mit &quot;Es war einmal&quot;, aber ich kenne auch ein Märchen, das anfängt mit &quot;Zu der Zeit, als das Wünschen noch geholfen hat ...&quot;.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Gibt es noch andere Beginnformeln und wenn ja, kann man diese dem geographischen Ursprung der Märchen zuordnen?&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;  &lt;p&gt;Many German fairy tales open with&#xA;  &quot;Es war einmal&quot;, but some start with&#xA;  &quot;Zu der Zeit, als das Wünschen noch&#xA;  geholfen hat ...&quot;.&lt;/p&gt;&#xA;  &#xA;  &lt;p&gt;Are there any other common&#xA;  introductions? If so, is there a correlation between their use and the geographic origin of the story?&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;" 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 &quot;es war einmal&quot;?" Tags="&lt;regional&gt;" AnswerCount="2" CommentCount="2" FavoriteCount="4" />

The problem itself is that when I retrieve the "Body" value - I always get a shortened string which is missing lot of characters/symbols. 问题本身是当我检索“正文”值时-我总是得到一个缩短的字符串,该字符串缺少很多字符/符号。

Take a look at the Body value. 看一下“ Body值。 It is 它是

Body="&lt;p&gt;Sehr viele Märchen beginnen auf Deutsch mit &quot;Es war einmal&quot;, aber ich kenne auch ein Märchen, das anfängt mit &quot;Zu der Zeit, als das Wünschen noch geholfen hat ...&quot;.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Gibt es noch andere Beginnformeln und wenn ja, kann man diese dem geographischen Ursprung der Märchen zuordnen?&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;  &lt;p&gt;Many German fairy tales open with&#xA;  &quot;Es war einmal&quot;, but some start with&#xA;  &quot;Zu der Zeit, als das Wünschen noch&#xA;  geholfen hat ...&quot;.&lt;/p&gt;&#xA;  &#xA;  &lt;p&gt;Are there any other common&#xA;  introductions? If so, is there a correlation between their use and the geographic origin of the story?&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;"

So, when I just print with System.out.println() I get only this 因此,当我仅使用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>

As you can see - the Body value contains html tags. 如您所见Body值包含html标签。 Can this be the problem? 这可能是问题吗? And how do I go about it? 我该怎么办呢?

Or maybe there are any other resolutions? 也许还有其他解决方法?

Thanks a lot! 非常感谢!

I suspect the problem is the &#xA;&#xA; 我怀疑问题是&#xA;&#xA; in your XML. 在您的XML中。 That's U+000A, or "line feed" (twice). 即U + 000A,或“换行”(两次)。 You can validate that this is the problem by taking the XML out of it entirely. 您可以通过完全删除XML来验证这是问题所在。 See what this does: 看看这是做什么的:

 System.out.println("Line 1\nLine2\nLine 3");

Depending on your console, that may appear on multiple lines. 根据您的控制台,它可能会显示在多行上。 That's what I'd expect to happen to your string from the XML as well. 我也希望XML中的字符串也发生这种情况。 My guess is either you only looked at the line starting with the start of your attribute text, or your console doesn't support multiline output. 我的猜测是您只看了以属性文本开头的那一行,或者您的控制台不支持多行输出。 Either way, the quick test above should give you an idea of what to expect. 无论哪种方式,上面的快速测试都应该使您对预期的想法有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM