现在我的软件将显示来自 xml 文件的(根元素:数据),但我需要节点列表。这是 Xml 文件所拥有的,例如:
(数据有“count”和“person”)
(计数 = 1)
(人有“名字”和“汽车”)
(名字=“亚当”)
(汽车有“Porshe”和“minimini”)
软装
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public void Ada()
{
File fXmlFile = new File("c:\\file.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());
}
XML 文件:
<data>
<count>1</count>
<person>
<name>Adam<name>
<cars>
<fast>Porshe</fast>
<slow>MiniMini</slow>
</cars>
</person>
</data>