gpt4 book ai didi

java - 无法获取 xml 文件中名称为 ="debitRequest"的父节点的子节点名称

转载 作者:行者123 更新时间:2023-12-01 17:25:12 25 4
gpt4 key购买 nike

我正在使用 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();
}
}
}

最佳答案

在给定的 XML 中,节点之间有文本(不可见):

<wsdl:message name="debitRequest"> <-- Text
<wsdl:part name="amount" type="xsd:float"/> <-- Text
<wsdl:part name="password" type="xsd:string"/> <-- Text
</wsdl:message>

这就是为什么名为 debitRequest 的节点有 5 个子节点(文本、节点、文本、节点、文本)

新建ChildNode的NodeType为1,Text的NodeType为3:

node.getNodeType()//1是ChildNode,3是Text

因此,在拥有节点并且想要获取其所有子节点之后,您应该循环它并检查类型。然后您可以检查属性。

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("testxml.xml");
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();
for(int j = 0; j < childNList.getLength();j++)
{
Node n = childNList.item(j);
if(n.getNodeType() == 1) //NodeType 1 = Next XML Node
{
String nvalue = n.getAttributes().getNamedItem("name").getNodeValue();
System.out.println(nvalue);
}
/*if(n.getAttributes() != null){
String s = n.getAttributes().getNamedItem("name").getNodeValue();
System.out.println(s);
}*/
}
System.out.println(childNList.getLength());

}
else{
System.out.println("NO CHILD FOUND for: "+valueOfTag);
}
}
}
} catch(Exception io) {
io.printStackTrace();
}
}
}

您还可以循环您的 child 并检查每个Node.getAttributes()(如果它为空)。如果不是,您可以在属性中搜索“name”并继续。第二种方式也显示在我的代码中,但被注释掉了。它是: if(n.getAttributes() != null){ 注释。

关于java - 无法获取 xml 文件中名称为 ="debitRequest"的父节点的子节点名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15501853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com