我是靠谱客的博主 斯文蜡烛,最近开发中收集的这篇文章主要介绍java 处理xml特殊字符,使用Java读取包含特殊字符(&,-等)的XML文档节点,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

My code does not retrieve the entirety of element nodes that contain special characters.

For example, for this node:

P&G Greenbelt

It would only retrieve "P" due to the ampersand. I need to retrieve the entire string.

Here's my code:

public List findTheaters() {

//Clear theaters application global

FilmhopperActivity.tData.clearTheaters();

ArrayList theaters = new ArrayList();

NodeList theaterNodes = doc.getElementsByTagName("theaterName");

for (int i = 0; i < theaterNodes.getLength(); i++) {

Node node = theaterNodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {

//Found theater, add to return array

Element element = (Element) node;

NodeList children = element.getChildNodes();

String name = children.item(0).getNodeValue();

theaters.add(name);

//Logging

android.util.Log.i("MoviefoneFetcher", "Theater found: " + name);

//Add theater to application global

Theater t = new Theater(name);

FilmhopperActivity.tData.addTheater(t);

}

}

return theaters;

}

I tried adding code to extend the name string to concatenate additional children.items, but it didn't work. I'd only get "P&".

...

String name = children.item(0).getNodeValue();

for (int j = 1; j < children.getLength() - 1; j++) {

name += children.item(j).getNodeValue();

}

Thanks for your time.

UPDATE:

Found a function called normalize() that you can call on Nodes, that combines all text child nodes so doing a children.item(0) contains the text of all the children, including ampersands!

解决方案

The & is an escape character in XML. XML that looks like this:

P&G Greenbelt

should actually be rejected by the parser. Instead, it should look like this:

P&G Greenbelt

There are a few such characters, such as < (<), > (>), " (") and ' ('). There are also other ways to escape characters, such as via their Unicode value, as in • or 〹.

For more information, the XML specification is fairly clear.

Now, the other thing it might be, depending on how your tree was constructed, is that the character is escaped properly, and the sample you showed isn't what's actually there, and it's how the data is represented in the tree.

For example, when using SAX to build a tree, entities (the &-thingies) are broken apart and delivered separately. This is because the SAX parser tries to return contiguous chunks of data, and when it gets to the escape character, it sends what it has, and starts a new chunk with the translated &-value. So you might need to combine consecutive text nodes in your tree to get the whole value.

最后

以上就是斯文蜡烛为你收集整理的java 处理xml特殊字符,使用Java读取包含特殊字符(&,-等)的XML文档节点的全部内容,希望文章能够帮你解决java 处理xml特殊字符,使用Java读取包含特殊字符(&,-等)的XML文档节点所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部