传入叶子节点,找父节点,递归调用。
public static String getXMLAbsolutePath(Element e) {
if (e.getParent() != null) {
return getXMLAbsolutePath(e.getParent()) + "." + e.getName();
} else {
return e.getName();
}
}
public static String getXMLAbsolutePath(Element e) {
if (e.getParent() != null) {
return getXMLAbsolutePath(e.getParent()) + "." + e.getName();
} else {
return e.getName();
}
}
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class main { public main() {
// TODO Auto-generated constructor stub
} private static boolean parseChild(Node node, String path) {
if (null == node) return false;
if (null != path && !path.equals(""))
path = path + "." + node.getNodeName();
else
path = node.getNodeName();
if (!node.getNodeName().startsWith("#"))
System.out.println(path);
NodeList nodelist = node.getChildNodes();
for (int i = 0; i < nodelist.getLength(); i++) {
Node child = (Node) nodelist.item(i);
parseChild(child, path);
}
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
Document document;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.txt");
Node mapTag = document.getChildNodes().item(0);
parseChild(mapTag, null);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}
test.txt 是你的xml内容
foreach (XElement node in doc.Element("Persons").Nodes())
{
if ("Person" == node.Name.LocalName)
{
//...
}
...
}