如果这是重复的话,请道歉。
我想显示Employees
的所有子属性
这是我的xml
<?xml version="1.0"?>
<Employees>
<Employee emplid="1111" type="admin">
<firstname>John</firstname>
<lastname>Watson</lastname>
<age>30</age>
<email>johnwatson@sh.com</email>
</Employee>
<Employee emplid="2222" type="admin">
<firstname>Sherlock</firstname>
<lastname>Homes</lastname>
<age>32</age>
<email>sherlock@sh.com</email>
</Employee>
<Employee emplid="3333" type="user">
<firstname>Jim</firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim@sh.com</email>
</Employee>
<Employee emplid="4444" type="user">
<firstname>Mycroft</firstname>
<lastname>Holmes</lastname>
<age>41</age>
<email>mycroft@sh.com</email>
</Employee>
</Employees>
这是java类
package com.logic.xpath;
import java.io.FileInputStream;
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.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Xpath
{
public static void main(String[] args)
{
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse( new FileInputStream("c:\\file.xml"));
/*String xml = ...;
Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));*/
XPath xPath = XPathFactory.newInstance().newXPath();
// Print all the employee email whose empid='3333'
String expression = "/Employees/Employee[@emplid='3333']/email";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++)
{
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
// Pritnt all the employees firstname
String expression1 = "/Employees/Employee/firstname";
System.out.println(expression1);
nodeList = (NodeList) xPath.compile(expression1).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++)
{
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
//Print all the employyees whose emp id is 2222
String expression3 = "/Employees/Employee[@emplid='2222']";
System.out.println(expression3);
Node node = (Node) xPath.compile(expression3).evaluate(document, XPathConstants.NODE);
if(null != node) {
nodeList = node.getChildNodes();
for (int i = 0;null!=nodeList && i < nodeList.getLength(); i++) {
Node nod = nodeList.item(i);
if(nod.getNodeType() == Node.ELEMENT_NODE)
System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue());
}
}
//Print all the employes firstname whose type is ADMIN
String expression4 = "/Employees/Employee[@type='admin']/firstname";
System.out.println(expression4);
nodeList = (NodeList) xPath.compile(expression4).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
//print all employees firstname whoes age is greater than 30
String expression5 = "/Employees/Employee[age>40]/firstname";
nodeList = (NodeList) xPath.compile(expression5).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
//print first name of first 2 employee define in the respective element
String expression6 = "/Employees/Employee[position() <= 2]/firstname";
nodeList = (NodeList) xPath.compile(expression6).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我想要display firstname lastname age email
String expression1 = "/Employees/Employee/firstname";
一样,只打印员工的名字,如何打印每件事
预期产出:
firstname=John lastname=Watson age=30 email=johnwatson@sh.com firstname=Sherlock lastname=Homes age=32 email=sherlock@sh.com and so on of every employee
答案 0 :(得分:0)
您可以尝试使用像
这样的StAX方法private final static String xml = <<your big xml>>;
public static void main(String... args) throws XMLStreamException {
// flag to indicate processing of Employee tag
boolean isEmployee = false;
// Flag to indicate processing text data in a tag rather than whitespaces outside tags,
// e.g. spaces between brackets
// <Employee emplid="3333" type="user">[
// ]<firstname>Jim</firstname>
boolean showText = false;
// current tag name;
String tagName = "";
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLEventReader reader = xif.createXMLEventReader(new StringReader(xml));
while (reader.hasNext()) {
XMLEvent ev = reader.nextEvent();
switch (ev.getEventType()) {
case START_ELEMENT:
StartElement start = ev.asStartElement();
tagName = start.getName().getLocalPart();
if (tagName.equalsIgnoreCase("Employee")) {
isEmployee = true;
} else if (isEmployee) {
showText = true;
}
break;
case CHARACTERS:
Characters chars = ev.asCharacters();
if (isEmployee && showText) {
System.out.println(tagName + " = " + chars.getData());
}
break;
case END_ELEMENT:
EndElement end = ev.asEndElement();
if (end.getName().getLocalPart().equalsIgnoreCase("Employee")) {
isEmployee = false;
System.out.println("");
}
showText = false;
break;
}
}
}
它需要Java 6+,可以从JavaDoc和Oracle
获得更多帮助答案 1 :(得分:0)
请尝试使用这些Xpath获取每个字段的单独值,保存任何变量以形成整个输出
//Employee[@emplid]/firstname/text()
//Employee[@emplid]/lastname/text()
//Employee[@emplid]/age/text()
//Employee[@emplid]/email/text()
要在xpath
下面使用所有文本//Employee[@emplid]//text()
答案 2 :(得分:0)
我找到了打印所有子元素的解决方案。
我只是改变了表达式
String expression1 = "/Employees/Employee/*";
System.out.println(expression1);
nodeList = (NodeList) xPath.compile(expression1).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++)
{
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
添加*
让我的工作