获取叶节点-Java中的XML解析
是否可以解析XML并获取所有叶节点获取叶节点-Java中的XML解析,java,xml,Java,Xml,是否可以解析XML并获取所有叶节点 <root> <emp> <name>abc<name> <age>12</age> </emp> <dept> <branch>cse</branch> </dept> </root> abc 12 cse 我的输出应该是 名称 年龄 branch读取xml非常简单 import java.io.File; i
<root>
<emp>
<name>abc<name>
<age>12</age>
</emp>
<dept>
<branch>cse</branch>
</dept>
</root>
abc
12
cse
我的输出应该是
名称
年龄
branch读取xml非常简单
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
导入java.io.File;
导入org.w3c.dom.Document;
导入org.w3c.dom.*;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入org.xml.sax.SAXException;
导入org.xml.sax.SAXParseException;
公共类ReadAndPrintXMLFile{
公共静态void main(字符串argv[]){
试一试{
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
Document doc=docBuilder.parse(新文件(“book.xml”);
//规范化文本表示
doc.getDocumentElement().normalize();
System.out.println(“文档的根元素是”+doc.getDocumentElement().getNodeName());
NodeList listOfPersons=doc.getElementsByTagName(“个人”);
int totalPersons=listOfPersons.getLength();
System.out.println(“总人数:“+totalPersons”);
对于(int s=0;s请尝试此代码…我将您的xml代码保存在桌面文件夹的e.xml中,并从您的xml中获取名称、年龄和分支
public static void main(String argv[]) {
try {
FileInputStream file = new FileInputStream(new File("C:/Users/devteam/Desktop/e.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("*************************");
String expression = "/root/emp/name";
String exp1="/root/emp/age";
String exp2="/root/dept/branch";
System.out.println(expression);
String name = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(exp1);
System.out.println(name);
System.out.println(exp1);
String age = xPath.compile(exp1).evaluate(xmlDocument);
System.out.println(age);
String branch = xPath.compile(exp2).evaluate(xmlDocument);
System.out.println(branch);
} catch (Exception e) {
e.printStackTrace();
}
}
输出:
*************************
/root/emp/name
/root/emp/age
abc
/root/emp/age
12
cse
使用此XPath表达式查找没有其他元素作为子元素的所有元素:/*[count(./*)=0]
try {
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("input.xml");
final XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//*[count(./*) = 0]");
final NodeList nodeList = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < nodeList.getLength(); i++) {
final Element el = (Element) nodeList.item(i);
System.out.println(el.getNodeName());
}
} catch (Exception e) {
e.printStackTrace();
}
检查此项。检查此处您的示例XML有一个错误:它应该是abc
而不是abc
。我知道如何解析XML。我只想单独获取叶节点。如何才能获得此结果对于任何给定的XML,它必须只返回叶节点,但这应该适用于任何XML。我无法硬编码像这样的Xpath,因为它似乎需要标记名,而不是标记的内容。@vanje:如果我想要标记名和具有属性的值,我应该对此代码做什么更改。
name
age
branch