Java 获取XML文件中所有属性的名称
嘿,我有一个XML文件,我想导航到一个给定的节点,并获取该节点所有属性的名称 例如:(XML文件) 但我想动态查找所有这些名称 提前谢谢Java 获取XML文件中所有属性的名称,java,Java,嘿,我有一个XML文件,我想导航到一个给定的节点,并获取该节点所有属性的名称 例如:(XML文件) 但我想动态查找所有这些名称 提前谢谢 -Scott元素.getAttributes()获取一个org.w3c.dom.NamedNodeMap。您可以使用item(int index)方法循环此过程,以获取org.w3c.dom.Attr节点,并从getName()方法获取这些节点的名称。您要查找的是元素。以下是如何获取XML中元素的示例: import java.io.*; import jav
-Scott
元素.getAttributes()
获取一个org.w3c.dom.NamedNodeMap
。您可以使用item(int index)
方法循环此过程,以获取org.w3c.dom.Attr
节点,并从getName()
方法获取这些节点的名称。您要查找的是元素。以下是如何获取XML中元素的示例:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class DOMElements{
static public void main(String[] arg){
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML File name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if(file.exists()){
// Create a factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
// Get a list of all elements in the document
NodeList list = doc.getElementsByTagName("*");
System.out.println("XML Elements: ");
for (int i=0; i<list.getLength(); i++) {
// Get element
Element element = (Element)list.item(i);
System.out.println(element.getNodeName());
}
}
else{
System.out.print("File not found!");
}
}
catch (Exception e) {
System.exit(1);
}
}
}
import java.io.*;
导入javax.xml.parsers.*;
导入org.w3c.dom.*;
导入org.xml.sax.*;
公营家庭用品{
静态公共void main(字符串[]arg){
试一试{
BufferedReader bf=新的BufferedReader(新的InputStreamReader(System.in));
System.out.print(“输入XML文件名:”);
字符串xmlFile=bf.readLine();
文件文件=新文件(xmlFile);
if(file.exists()){
//创建工厂
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
//使用工厂创建生成器
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(xmlFile);
//获取文档中所有元素的列表
节点列表=doc.getElementsByTagName(“*”);
System.out.println(“XML元素:”);
对于(int i=0;我只是一个术语注释……在您上面所写的内容中,
等标记实际上是包含文本的XML元素,它们不是属性。属性看起来像:
,以防您在设计XML。这里有一篇关于何时选择属性与何时选择元素的文章:谢谢ou,我是XML新手,这些都很有帮助
File file = new File("data.xml");
try
{
/* Parse File */
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
/* Find Starting Tag */
NodeList nodes = doc.getElementsByTagName(StartTag);
for (int i = 0; i < nodes.getLength(); i++)
{
Element element = (Element) nodes.item(i);
System.out.println(element);
}
String name = element.getAttribute("Attribute1");
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class DOMElements{
static public void main(String[] arg){
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML File name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if(file.exists()){
// Create a factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
// Get a list of all elements in the document
NodeList list = doc.getElementsByTagName("*");
System.out.println("XML Elements: ");
for (int i=0; i<list.getLength(); i++) {
// Get element
Element element = (Element)list.item(i);
System.out.println(element.getNodeName());
}
}
else{
System.out.print("File not found!");
}
}
catch (Exception e) {
System.exit(1);
}
}
}