使用 java XML 分离子节点
Posted
技术标签:
【中文标题】使用 java XML 分离子节点【英文标题】:Separating Child Nodes using java XML 【发布时间】:2017-01-27 03:07:18 【问题描述】:我应该如何分离仅包含“电子邮件”标签的“学生”节点。下面的 xml 包含学生标签中的各种子节点,如姓名、班级、地址和电子邮件。但是一个学生标签只包含“电子邮件”。我应该如何使用 java XML 仅分离特定的 Student 标签。请帮忙,我是 Java XML 的新手。
<Student>
<name>Sample1</name>
<class>1</class>
<address>2525</address>
<email>sample1@sample.com</email>
</Student>
<Student>
<name>Sample2</name>
<class>2</class>
<address>2153</address>
<email>sample2@sample.com</email>
</Student>
<Student>
<email>sample3@sample.com</email>
</Student>
【问题讨论】:
带上所有学生,遍历所有学生,如果一个人没有名字、没有班级、没有地址,那就是那个学生。 @Arun。我已经创建了一个示例示例来满足您的要求。如果您需要更多详细信息,请告诉我 @mhasan。非常感谢..!!现在我得到了我想要的…… 请接受解决方案并标记为+1 【参考方案1】:试试这个。
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Parser
private static String XPATH_EXPRESSION_STRING = "//Students/Student[not ((name) and (class) and (address))]";
public static void main(String[] args)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try
builder = factory.newDocumentBuilder();
doc = builder.parse("C:\\Users\\NPGM81B\\Desktop\\Students.xml");
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();
// Create XPath object
XPath xpath = xpathFactory.newXPath();
XPathExpression expr = xpath.compile(XPATH_EXPRESSION_STRING);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
Node node = nodes.item(i);
printNode(node, "********");
catch (ParserConfigurationException e)
e.printStackTrace();
catch (SAXException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (XPathExpressionException e)
// TODO Auto-generated catch block
e.printStackTrace();
private static void printNode(Node rootNode, String spacer)
System.out.println(spacer + rootNode.getNodeName() + " -> " + rootNode.getNodeValue());
NodeList nl = rootNode.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
printNode(nl.item(i), spacer + " ");
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<name>Sample1</name>
<class>1</class>
<address>2525</address>
<email>sample1@sample.com</email>
</Student>
<Student>
<name>Sample2</name>
<class>2</class>
<address>2153</address>
<email>sample2@sample.com</email>
</Student>
<Student>
<email>sample3@sample.com</email>
</Student>
</Students>
【讨论】:
【参考方案2】:XPath 表达式仅选择不包含名称、类和地址子节点的学生节点。 '/学生[not(name) and not(class) and not(address)]'。不太确定您的约束和要求到底是什么,但此表达式将在您的示例中起作用。对于 Java 的简单代码示例,有很多很好的教程,例如:https://www.tutorialspoint.com/java_xml/java_xpath_parse_document.htm
【讨论】:
以上是关于使用 java XML 分离子节点的主要内容,如果未能解决你的问题,请参考以下文章
easyui tree 怎么获取选择节点子节点上的值?js怎么写?