Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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获取不基于命名空间的xml标记_Java_Xml_Xml Parsing_Nodelist - Fatal编程技术网

使用Java获取不基于命名空间的xml标记

使用Java获取不基于命名空间的xml标记,java,xml,xml-parsing,nodelist,Java,Xml,Xml Parsing,Nodelist,我将这个响应字符串作为XML <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:SRVResponse> <ns:Response> <ns1:ServiceHeader> <ns1:rsHeader> <ns1:status&g

我将这个响应字符串作为XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
  <ns:SRVResponse>
  <ns:Response>
    <ns1:ServiceHeader>
       <ns1:rsHeader>
          <ns1:status>
             <ns1:finStatus>E</ns1:finStatus>
             </ns1:status>
        </ns1:rsHeader>
    </ns1:ServiceHeader>
</ns:Response>
</ns:SRVResponse>
</soapenv:Body>
</soapenv:Envelope>
这只是因为我将标记作为ns1:finStatus传递

如何实现这一点,而不是基于名称空间标记

1)您已经在使用命名空间获取节点。所以我看不出有什么问题

2) 在第二个方法中也使用*search,即在getElementTextContent()中,使用
el.getElementsByTagnames(“*”,elemTag)

3) 要在将ns:前缀传递给getElementTextContent之前跳过它,请使用
getLocalName()
而不是
getNodeName()

1)您已经在使用命名空间获取节点。所以我看不出有什么问题

2) 在第二个方法中也使用*search,即在getElementTextContent()中,使用
el.getElementsByTagnames(“*”,elemTag)


3) 要在将ns:前缀传递给getElementTextContent之前跳过它,请使用
getLocalName()
而不是
getNodeName()

安装XPath 2.0库并执行
/*:finStatus
。通过DOM导航实现这一点只是受虐狂。要么是这样,要么是因为您编写的代码行数而获得报酬。

安装XPath 2.0库并执行
/*:finStatus
。通过DOM导航实现这一点只是受虐狂。要么是这样,要么是因为编写的代码行数而付费。

el.getElementsByTagName(“*”,elemTag)不能将两个参数作为字符串。对不起,我是说GetElementsByTagnamesyeah得到了它。谢谢,但我看不到您的xml中的总体状态。您可以直接获取该元素本身,而不是首先获取它的父元素。没有?对不起,是打字错误。应该是finStatus。在questionel中更新。getElementsByTagName(“*”,elemTag),不能将两个参数作为字符串。抱歉,我的意思是GetElementsByTagnamesyeah得到了它。谢谢,但我看不到您的xml中的总体状态。您可以直接获取该元素本身,而不是首先获取它的父元素。没有?对不起,是打字错误。应该是finStatus。在问题编号中更新。我来这里是告诉你如何解决你的问题,而不是为你做你的工作。谢谢@Michael.No。我在这里告诉你如何解决你的问题,而不是为你做你的工作。谢谢你@Michael。
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                           dbf.setNamespaceAware(true);
                           DocumentBuilder db = dbf.newDocumentBuilder();
                           InputSource is = new InputSource();                                 
                            is.setCharacterStream(new StringReader(strResponse));
                            if(is != null) {
                                Document doc = db.parse(is);                                

                                NodeList idDetails =  doc.getDocumentElement().getElementsByTagNameNS("*", "status");
                                if(idDetails != null) {
                                    int length = idDetails.getLength();
                                    for (int i = 0; i < length; i++) {
                                        if (idDetails.item(i).getNodeType() == Node.ELEMENT_NODE) {
                                            Element el = (Element) idDetails.item(i);
                                            if (el.getNodeName().contains("status")) {
                                                status = getElementTextContent(el, "ns1:finStatus");
                                                System.out
                                                        .println("Status :"+status);
                                            }
                                        }
                                    }
                                }
                            }
public static String getElementTextContent(Element el, String elemTag) {
    String result = "";
    if(el.getElementsByTagName(elemTag) != null) {
        if(el.getElementsByTagName(elemTag).item(0) != null) {
            if(el.getElementsByTagName(elemTag).item(0).getTextContent() != null) {
                result = el.getElementsByTagName(elemTag).item(0).getTextContent();
            } else {
                result = "";
            }
        }
    }
    return result;
}