Java 如何从DOM文档(元素、属性等)获取所有节点
我正在尝试获取文档中的所有节点,并且只能使用Java 如何从DOM文档(元素、属性等)获取所有节点,java,dom,Java,Dom,我正在尝试获取文档中的所有节点,并且只能使用getElementByTagName(“*”)获取元素节点。但这不会返回其他节点,即。属性和其他节点 是否有可用的api,或者我必须进一步迭代这些元素才能获得属性节点 这就是我现在正在尝试的;需要知道是否有其他方法或直接api用于相同的目的 private static List<Node> getAllNodes(Document doc) { List<Node> returnList = new Link
getElementByTagName(“*”)
获取元素节点。但这不会返回其他节点,即。属性和其他节点
是否有可用的api,或者我必须进一步迭代这些元素才能获得属性节点
这就是我现在正在尝试的;需要知道是否有其他方法或直接api用于相同的目的
private static List<Node> getAllNodes(Document doc) {
List<Node> returnList = new LinkedList<>();
NodeList nodes = doc.getElementsByTagName("*");
for (int index = 0; index < nodes.getLength(); index++) {
returnList.add(nodes.item(index));
NamedNodeMap attribList = nodes.item(index).getAttributes();
if (attribList == null) {
continue;
}
for (int j = 0; j < attribList.getLength(); j++) {
returnList.add(attribList.item(j));
}
}
return returnList;
}
私有静态列表getAllNodes(文档文档){
List returnList=新建LinkedList();
NodeList节点=doc.getElementsByTagName(“*”);
对于(int index=0;index
提前感谢您必须使用它来提供所有节点(属性、元素ecc…)
文档文档//您的文档类
印刷儿童(doc);
公共void printChild(节点)
{
NodeList childNodes=node.getChildNodes();
System.out.println(“节点:“+Node.getNodeType()+”,“+Node.getLocalName());
对于(int i=0;i
如果您使用的是selenium(只是猜测),那么应该使用findelelements(By.xpath(“”)),标记名不被视为regex,这就是为什么“”不起作用的原因。这对我来说是有效的
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
//your file object put here.
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("*");
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
Element element = (Element) node;
System.out.println(element.getNodeName())
}
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(文件);
//您的文件对象放在这里。
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“*”);
对于(int i=0;i
编辑:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
//your file object put here.
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("*");
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
Element element = (Element) node;
System.out.println(element.getNodeName());
String name = element.getAttribute("name");
System.out.println(name);
}
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(文件);
//您的文件对象放在这里。
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“*”);
对于(int i=0;i
请粘贴您的dom节点文件。请注意,属性的语义随[DOM4]而改变(属性不再是节点),它们不再实现节点接口。这可能与java无关,但DOM树的节点和附加到节点的属性之间的语义区别仍然需要记住。这将返回属于元素类型的所有节点,我也需要这些元素属性的所有属性。请将您的文档粘贴到此处。这一要求适用于任何xml。在安娜的情况下,我应该能够得到“person”节点“sex”节点和“firstname”节点请查看编辑。您可以按名称获取属性。提供所有节点,但仅限于当前节点。对于文档节点,我只得到文档节点的直接子节点,我需要该文档中的所有节点
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
//your file object put here.
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("*");
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
Element element = (Element) node;
System.out.println(element.getNodeName());
String name = element.getAttribute("name");
System.out.println(name);
}