- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
有谁知道如何实现称为 PRUNING 的图像处理技术?任何想法,例子等?我正在使用 OpenCV 和 C#,如果有人可以提供帮助,我将不胜感激。 最佳答案 我假设您希望从图像中移除不需要的杂散和伪影。
假设我已经构建了一个可调用对象数组 callables = [] callables << block1 callables << block2 callables << block3 callabl
这个问题在这里已经有了答案: std::shared_ptr of this (2 个答案) 关闭 8 年前。 我正在学习 C++11 特性,特别是 shared_ptr,我在引用 this 并将其
我主要是一个oracle新手,所以如果这是一个愚蠢的问题,请原谅我...... 我已经阅读了很多关于 authID 的文章,我的理解是 AUTHID 子句指示 Oracle 是否以调用者的权限 (CU
我使用的是 Python 2.7。 按照惯例,__iter__、__init__、__str__ 等内置方法使用双下划线来标识它们。为什么 next() 函数没有这个? 它与这里的其他内置函数一起
这是疏忽吗?还是跟JVM有关? 最佳答案 Java 确实有指针——不能在指针上执行指针运算。 来自可敬的JLS : There are two kinds of types in the Java p
我越来越多地发现宣传“原生”桌面应用程序的服务,但在下载或研究后发现它们是在 Electron 或类似的 Web View 框架中构建的 Web View 。他们误解了“本地人”还是我? 另外,我可以
我尝试在 Wordpress 中使用,但它在控制台中显示 jstree 不是函数在 header PHP 文件中: /dist/libs/jquery.js"> /dist/themes/defaul
我想让 scons 调用带有目录列表的 nosetests。执行此操作的最佳方法是什么? 最佳答案 如果您需要分析外部应用程序的返回码(例如,如果您调用测试),您需要使用 Command() + py
这是一些尝试使用 OOB(紧急)数据的基本代码。我的问题是,如果客户端使用 C 或 Java,则服务器部分的行为不同。请注意,您可能认为这两个客户端都有些棘手,但如果我使用 C 服务器(以更好地控制
虚拟文件系统 (VFS) 或虚拟文件系统交换机是更具体文件系统之上的抽象层。 VFS的目的是允许客户端应用程序以统一的方式访问不同类型的具体文件系统。 如果我们看到 VFS 的实际工作,这个定义似乎是
"Effective Go"状态: By convention, one-method interfaces are named by the method name plus an -er suff
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 3 个月前。 社区审核了是否
在深入研究 Scala 时,我遇到了 这个词。类型类 . 它一直令人困惑,因为一个类是一个类型,而一个类型可以 在 Scala 中是一个类,“类型”和“类”本身就是抽象术语。 在阅读了一些示例后,我感
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
我正在尝试编写一个简单地杀死我的程序的函数。它被称为退出聊天。但是,当我调用函数 messenger:quitChat() 时,出现此错误。 ** exception error: bad argum
如果您向 DB2/zOS 引擎 DBA 询问有关 DB2 行为的问题,DBA 会将 DB2 引擎称为“他”,就像水手使用“她”来指代他的船一样。 例如:“一旦您填充了可用空间,DB2 仍希望在表空间中
案例:我有 2 个具有以下 OneToMany 关系的表:TagAbstract 1..* TagConf,其中: tag_abstract_ID 是一个字段,用于标识许多 TagConf 记录,这些
我有一个表格,我想让第一行“自动固定”(这意味着当向下滚动时它固定在顶部,而如果不向下滚动,它就不固定)。这是代码:``` table { background: yellow; } td
其他动词对我来说都有意义,但我对“发布”作为动词没有太多上下文。它是像在邮局发帖(这有一定道理,虽然看起来有点牵强)还是像在公告板上发帖(对我来说意义不大)?有谁知道是谁决定了“POST”以及为什么选
我是一名优秀的程序员,十分优秀!