private void dislpay() {
try {
File fXmlFile = new File("/data/data/com.example.addnode/Add.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("details");
for (int temp = 0; temp <= nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String a=eElement.getElementsByTagName("firstname").item(temp).getTextContent().toString();
String b=eElement.getElementsByTagName("lastname").item(temp).getTextContent().toString();
String c=eElement.getElementsByTagName("nickname").item(temp).getTextContent().toString();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
} catch (Exception e) {
e.printStackTrace();
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<user>
<details id="1">
<firstname>JOHN</firstname>
<lastname>R</lastname>
<nickname>JJ</nickname>
</details>
<details>
<firstname>NOMAN</firstname>
<lastname>K</lastname>
<nickname>NK</nickname>
</details>
</user>
预期产出:
JOHN R JJ
NOMAN K NK
当前输出为:
JOHN R JJ
我想显示子节点的所有值(详细信息),但是当我执行应用程序时,它只显示前三个值而不是全部。我正在学习XML,所以我对XML知之甚少。所以请指导我。
答案 0 :(得分:0)
在现有代码中,纠正错误....,
//Remove = otherwise loop will run 3 times and you will get null pointer exception
for (int temp = 0; temp < nList.getLength(); temp++)
// Replace temp with 0 here because temp is increasing with your for loop and this is first node of this element always
String a=eElement.getElementsByTagName("firstname").item(0).getTextContent().toString();
String b=eElement.getElementsByTagName("lastname").item(0).getTextContent().toString();
String c=eElement.getElementsByTagName("nickname").item(0).getTextContent().toString();
为了您的方便, 我自己准备了这个DOM解析器,使用递归来解析你的xml,而不需要知道单个标记。如果存在,它将按顺序为您提供每个节点的文本内容。您可以删除以下代码中的注释部分以获取节点名称。希望它会有所帮助。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class RecDOMP {
public static void main(String[] args) throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
// replace following path with your input xml path
Document doc = db.parse(new FileInputStream(new File ("D:\\ambuj\\ATT\\apip\\APIP_New.xml")));
// replace following path with your output xml path
File OutputDOM = new File("D:\\ambuj\\ATT\\apip\\outapip1.txt");
FileOutputStream fostream = new FileOutputStream(OutputDOM);
OutputStreamWriter oswriter = new OutputStreamWriter (fostream);
BufferedWriter bwriter = new BufferedWriter(oswriter);
// if file doesnt exists, then create it
if (!OutputDOM.exists()) {
OutputDOM.createNewFile();}
visitRecursively(doc,bwriter);
bwriter.close(); oswriter.close(); fostream.close();
System.out.println("Done");
}
public static void visitRecursively(Node node, BufferedWriter bw) throws IOException{
// get all child nodes
NodeList list = node.getChildNodes();
for (int i=0; i<list.getLength(); i++) {
// get child node
Node childNode = list.item(i);
if (childNode.getNodeType() == Node.TEXT_NODE)
{
//System.out.println("Found Node: " + childNode.getNodeName()
// + " - with value: " + childNode.getNodeValue()+" Node type:"+childNode.getNodeType());
String nodeValue= childNode.getNodeValue();
nodeValue=nodeValue.replace("\n","").replaceAll("\\s","");
if (!nodeValue.isEmpty())
{
System.out.println(nodeValue);
bw.write(nodeValue);
bw.newLine();
}
}
visitRecursively(childNode,bw);
}
}
}