Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getNodeValue()截断org.w3c.dom.Node中的属性内容_Java_Android_Xml_String_Dom - Fatal编程技术网

Java getNodeValue()截断org.w3c.dom.Node中的属性内容

Java getNodeValue()截断org.w3c.dom.Node中的属性内容,java,android,xml,string,dom,Java,Android,Xml,String,Dom,我正在Android上工作,需要从URL获取XML并检索一些值。下载是可以的,但有些字段可以包含HTML实体(如–)。当我从节点类(org.w3c.dom.Node)调用getNodeValue()方法时,该值在找到&char时停止,并截断字符串 例如: 第56集和第8211集;英雄 当我调用getNodeValue()时,只返回“插曲56”。您可以尝试以下方法 String str = "<title>Episode #56 &#8211; Heroes</titl

我正在Android上工作,需要从URL获取XML并检索一些值。下载是可以的,但有些字段可以包含HTML实体(如–)。当我从节点类(org.w3c.dom.Node)调用getNodeValue()方法时,该值在找到&char时停止,并截断字符串

例如:

第56集和第8211集;英雄

当我调用getNodeValue()时,只返回“插曲56”。

您可以尝试以下方法

String str = "<title>Episode #56 &#8211; Heroes</title>";
str = str.replaceAll("&", "amp;");
String str=“第56集和第8211集;英雄”;
str=str.replaceAll(&,“amp;”);
然后尝试解析'str',应该可以

下面是使用纯dom解析器的示例实现

public static void main(String[] args) throws XPathExpressionException {
    String str = "<title>Episode #56 &#8211; Heroes</title>";   
    str = str.replaceAll("&", "amp;");
    Document domDoc = null;
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
        domDoc = docBuilder.parse(bis);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NodeList nlist = domDoc.getElementsByTagName("title");
    //System.out.println("child count  "+nlist.getLength());
    System.out.println("title value = "+nlist.item(0).getTextContent());
}
publicstaticvoidmain(String[]args)抛出XPathExpressionException{
String str=“第56集和第8211集;英雄”;
str=str.replaceAll(&,“amp;”);
文档domDoc=null;
试一试{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
ByteArrayInputStream bis=新的ByteArrayInputStream(str.getBytes());
domDoc=docBuilder.parse(bis);
}捕获(例外e){
e、 printStackTrace();
}
NodeList nlist=domDoc.getElementsByTagName(“标题”);
//System.out.println(“子计数”+nlist.getLength());
System.out.println(“title value=“+nlist.item(0).getTextContent());
}

我尝试了这个方法,但没有成功:String title=parser.getValue(e,KEY\u title);myObj.setTitle(title.replaceAll(“&”,“amp;”);//同样的问题。@Rafael我有一个给定的示例代码,但是没有用dalvic运行时测试,但是逻辑可以正确实现?。
public static void main(String[] args) throws XPathExpressionException {
    String str = "<title>Episode #56 &#8211; Heroes</title>";   
    str = str.replaceAll("&", "amp;");
    Document domDoc = null;
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
        domDoc = docBuilder.parse(bis);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NodeList nlist = domDoc.getElementsByTagName("title");
    //System.out.println("child count  "+nlist.getLength());
    System.out.println("title value = "+nlist.item(0).getTextContent());
}