- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 xml 节点获取文本。该代码似乎可以识别该节点。这段代码 String L = "节点长度:"+ nList.getLength()+ "文本:"+ nList.item(0).toString(); jTextArea1.setText(L);
返回:节点长度:1 文本:[公司名称:null]
所以看起来代码正在查找节点但没有获取值。这是整个代码块(这是我第一次发帖,所以我希望我的格式正确!)。 FOR 循环应该获取该值,但抛出 NULL 指针异常:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//Build Document
Document xdocument = builder.parse(new File("request.xml"));
xdocument.getDocumentElement().normalize();
NodeList nList = xdocument.getElementsByTagName("CompanyName");
//String L = "Node Length: " + nList.getLength()+ " Text: " + nList.item(0).toString();
//jTextArea1.setText(L);
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
String nodetxt= "Company : " + eElement.getElementsByTagName("CompanyName").item(0).getTextContent() ;
jTextArea1.setText(nodetxt) ;
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(TechKnowPOSGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RunReportQueryAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CompanyName>Techknow</CompanyName>
<IntegrationLoginId>cwapitest</IntegrationLoginId>
<IntegrationPassword>cwtest123</IntegrationPassword>
<ReportName>Company</ReportName>
<!-- <Conditions></Conditions> -->
<!-- <Limit>10</Limit> -->
<!-- <Skip></Skip> -->
<!-- <OrderBy></OrderBy> -->
</RunReportQueryAction>
非常感谢任何帮助。
最佳答案
在此行中,您可以获得所有 CompanyName
元素:
NodeList nList = xdocument.getElementsByTagName("CompanyName");
然后你在 for 循环中循环它们并调用:
eElement.getElementsByTagName("CompanyName")
但这意味着 CompanyName
具有嵌套的 CompanyName
元素,但事实并非如此。因此,您应该在 for 循环中使用它,因为您正在迭代的元素已经是 CompanyName
元素:
String nodeTxt = "Company : " + eElement.getTextContent()
关于java - xml 节点上的 getTextContent 返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001526/
所以我一直在尝试按预订顺序收集所有节点名称及其内容。因此,我使用递归方法从 XML 文件中获取所有节点以及文本。问题是每当我执行它时,我都会不断在 ArrayList 中获取空字符串。空字符串位于 A
当我尝试从我的 servlet 的 doGet 方法访问我的 xml 数据时,它只输出不超过空格的值,包括整个值。 XML 文件: Apartment
我试图只获取顶级文本,而不获取任何子文本。所以我有以下 xml: text1 text2 我只想得到父(text1)文本。所以在这个例子中我会做 node.getTextContent
我正在尝试从 xml 节点获取文本。该代码似乎可以识别该节点。这段代码 String L = "节点长度:"+ nList.getLength()+ "文本:"+ nList.item(0).toSt
我有以下XML和Xpath代码..使用node.getContent()我得到完整SUID元素的字符串返回..获取每个元素并使用recid,suid构造一个新对象的最佳方法是什么,组 谢谢!
下面是抛出 AbstractMethodError 的示例代码块: import org.w3c.dom.Node; .. Node root = soapBody.getElementByTagNa
Node.getTextContent() 返回当前节点及其子节点的文本内容。 有没有办法获取当前节点的文本内容,而不是后代的文本。 例子 XML is a browser
我正在尝试在 JUnit 测试中构建 xml 文档。 doc=docBuilder.newDocument(); Element root = doc.createElement("Setting
我正在尝试将 ckfinder 合并到我的 Apache/Tomcat 系统中。我遇到的问题是,当配置文件尝试读取 configuration.xml 时,它会很好地返回节点,但在节点上调用 getT
有人遇到过这个问题吗? 我在 Java 7 中使用 javax.xml.soap.SOAPElement 的 getTextContent()。但是当我使用 Java 8 时,它给出编译错误“方法 g
在我的项目中,我遇到了一个问题 The method getTextContent() is undefined for the type Node 我目前正在使用 jdk 1.5,谁能告诉我这是怎么
我有使用 DOM 解析器解析的 xml 配置文件。当我使用 node.getTextContent(); 从子节点获取值时,我无法删除字符串 value 中的空格。当我给出没有空格的值时,它会起作用。
我使用的是旧版本的 JRE (1.4),其中 Node.getTextContents() 和 Node.setTextContents() 不可用。做这些 Action 还有很长的路要走吗? 示例
java 源代码不能用 jdk 6 编译。 import org.w3c.dom.Node; Node node = list.item(0); String txtContent = node
本文整理了Java中org.opensaml.xml.schema.impl.XSAnyImpl.getTextContent()方法的一些代码示例,展示了XSAnyImpl.getTextConte
本文整理了Java中org.opensaml.core.xml.schema.XSAny.getTextContent()方法的一些代码示例,展示了XSAny.getTextContent()的具体用
我编写了一些 java 代码,使用 DOM 解析 XML,以便在我的程序中加载数据。使用 Eclipse“format”函数格式化 XML 时,我遇到了一个问题:以前从文档元素中工作的 getText
本文整理了Java中org.opendaylight.controller.netconf.util.xml.XmlElement.getTextContent()方法的一些代码示例,展示了XmlEl
本文整理了Java中org.opendaylight.controller.config.util.xml.XmlElement.getTextContent()方法的一些代码示例,展示了XmlEle
我遇到了 org.w3c.dom 的 Node.getTextContent() 的问题。我有以下代码块: String name = document.getElementsByTagName("n
我是一名优秀的程序员,十分优秀!