有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法获取xml文件中具有name=“debitRequest”的父节点的子节点名称

我使用的是xml文件“SavedWSDL.txt”,下面给出了其中的一些部分

  ...
<wsdl:message name="LookUpTransactionResponse">
<wsdl:part name="LookUpTransactionReturn" type="impl:ArrayOf_xsd_anyType"/>
</wsdl:message>
<wsdl:message name="LookUpTransactionRequest"></wsdl:message>
<wsdl:message name="creditResponse">
<wsdl:part name="creditReturn" type="xsd:int"/>
</wsdl:message>
<wsdl:message name="creditRequest">
<wsdl:part name="amount" type="xsd:float"/>
<wsdl:part name="password" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="debitRequest">
<wsdl:part name="amount" type="xsd:float"/>
<wsdl:part name="password" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="debitResponse">
<wsdl:part name="debitReturn" type="xsd:int"/>
</wsdl:message>
    ...

我已经编写了下面给出的java代码,应该使用什么来获取名为“debitRequest”的wsdl:message标记的子节点名

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Ex2 {

    public static void main(String[] args) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse("D:/SavedWSDL.txt");
            doc.getDocumentElement().normalize();

            NodeList nodelist = doc.getElementsByTagName("wsdl:message");
            //System.out.println("No. of Nodes: "+nodelist.getLength());

            for(int i=0;i<nodelist.getLength();i++){
                Node node=nodelist.item(i);
                String valueOfTag=node.getAttributes().getNamedItem("name").getNodeValue();
                if(valueOfTag.equalsIgnoreCase("debitrequest")){
                    if(node.hasChildNodes()){
                        NodeList childNList=node.getChildNodes();
                        //System.out.println("No. of Childs: "+node.getChildNodes().getLength());
                        //System.out.println(node.getAttributes().getNamedItem("name").getNodeValue());
                    }
                    else{
                        System.out.println("NO CHILD FOUND for: "+valueOfTag);
                    }
                }   
            }
        } catch(Exception io) {
            io.printStackTrace();
        } 
    }
    }

共 (0) 个答案