简体   繁体   English

检索XML节点名称

[英]Retrieving XML node names

I am trying to retrieve the names of all the nodes from XML file using "node.getNodeName()". 我试图使用“node.getNodeName()”从XML文件中检索所有节点的名称。 While doing so, every node name is preceeded and followed by "#text". 执行此操作时,每个节点名称都以“#text”开头,后跟“#text”。 Because of that, i am not getting the exact count of nodes as well. 因此,我也没有得到确切的节点数。 I want "#text" to be eliminated while retrieving the names. 我希望在检索名称时删除“#text”。 How do i do that?? 我怎么做??

With that : 接着就,随即 :

package com.hum;

import java.io.InputStreamReader;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author herve
 */
public class PrintNameXML
{
  public static void main(String[] args) throws Exception
  {
    String xml = "<a><o><u>ok</u></o></a>";
    Document doc = 
     DocumentBuilderFactory
     .newInstance()
     .newDocumentBuilder()
     .parse(new InputSource(new StringReader(xml)));
    NodeList nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++)
    {
      System.out.println("name is : "+nl.item(i).getNodeName());
    }
  }
}

I get : 我明白了:

name is : a
name is : o
name is : u

Is that you search ? 你在搜索吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM