为什么此 XML 标记的属性计数为 0
我正在使用 STAX Parser 来解析 XML 文档。 我有下面这个标签,
<bustxml><![CDATA[<bustxml xmlns=\"http://www.bustprotocol.org/bustxml-5-0-SP2\"><NewOrdMBag TmInForce=\"0\" OrdTyp=\"1\" Acct=\"1234\"><Ord OrdQty=\"1\" </bustxml>
我需要读取上面标签的属性,所以我使用了
case XMLStreamConstants.START_ELEMENT:
for(int i = 0, n = reader.getAttributeCount(); i < n; ++i)
System.out.println("Attribute: " + reader.getAttributeName(i)
+ "" + reader.getAttributeValue(i));
但不幸的是我得到了,Attrbute Count 为 0 。 请告诉我如何读取bustxml标签内的所有内容
I am using STAX Parser for parsing the XML documents .
I have this below tag
<bustxml><![CDATA[<bustxml xmlns=\"http://www.bustprotocol.org/bustxml-5-0-SP2\"><NewOrdMBag TmInForce=\"0\" OrdTyp=\"1\" Acct=\"1234\"><Ord OrdQty=\"1\" </bustxml>
I need to read the attributes of the above tag , so i used
case XMLStreamConstants.START_ELEMENT:
for(int i = 0, n = reader.getAttributeCount(); i < n; ++i)
System.out.println("Attribute: " + reader.getAttributeName(i)
+ "" + reader.getAttributeValue(i));
But unfortunately i am getting , Attrbute Count as 0 .
Please tell me how can i read all the contents inside the bustxml tag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bustxml
元素没有属性,如果有的话,看起来会像这样:如果你想读取
bustxml
元素的内容,那么你应该使用getElementText< /代码> 相反。
更新:如果您正在谈论 CDATA 部分中包含的编码 xml 片段中的
bustxml
元素,那么您需要提取此 CDATA 片段的文本,然后解析/提取取而代之的是它的属性。The
bustxml
element has no attributes, if it did it would look like this:If you want to read the content of the
bustxml
element then you should usegetElementText
instead.Update: If you are talking about the
bustxml
element in the encoded xml fragment contained in the CDATA section then you need to extract the text of this CDATA fragment and then parse / extract attributes from it instead.CDATA 外部的bustxml 元素没有属性。
CDATA 内的bustxml 不是标签。这就是 CDATA 的含义:“这是字符数据。不要将您在此处找到的任何内容视为标记。”因此,内容可能看起来像标签,但由于 CDATA,它不是标签,因此它没有属性。
不幸的是,人们采用 XML 并将其包装在 CDATA 中的情况很常见。他们这样做是为了让你的生活变得困难。 (好吧,我想这就是原因,我想不出任何其他原因。)当发生这种情况时,您唯一的补救措施是提取 CDATA 中的文本并将其提交给 XML(或 HTML)解析器以将其转换为树,然后您就可以以正常方式访问元素和属性。
就您而言,他们不仅将其包装在 CDATA 中,而且还对其进行转义,从而使其变得更加困难。所以他们有效地对其进行了双重转义,因此您必须将其通过解析器两次(除了原始解析之外)才能理解它。
我会大声向那些给你发送这些垃圾的人抱怨。
The bustxml element outside the CDATA has no attributes.
The bustxml inside the CDATA is not a tag. That's what CDATA means: "This is character data. Don't treat anything you find in here as markup." So the content might look like a tag, but it's not a tag, because of the CDATA, and therefore it has no attributes.
Unfortunately it's very common for people to take XML and wrap inside CDATA. They do this to make your life difficult. (Well I assume that's the reason, I can't think of any other.) When this happens your only remedy is to extract the text inside the CDATA and submit it to an XML (or HTML) parser to turn it into a tree, and then you can access the elements and attributes in the normal way.
In your case they've made it doubly difficult by not only wrapping it in CDATA, but escaping it as well. So they've effectively double-escaped it, so you're going to have to put it through the parser twice (in addition to the orginal parse) to make sense of it.
I would complain very loudly to the people who sent you this rubbish.