问题
Hi I have requirement like parsing xml and get all child nodes and there data between nodes <Employees> and </Employees>
I have xml like:
<?xml version="1.0"?>
<Employees>
<Employee emplid="1111" type="admin">
<firstname>test1</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>
</Employees>
I need response like
<Employee emplid="1111" type="admin">
<firstname>test1</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>
I have tried below code
FileInputStream file = new FileInputStream(new File("E:\\test.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("*************************");
String expression = "/Employees/*";
System.out.println(expression);
String email = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(email);
but I am getting response like
test1
Watson
30
johnwatson@sh.com
I have used expression like /Employees/* but its not working
can anyone help me in doing this?
回答1:
This would be a possible XSLT transformation:
<xsl:template match="Employees">
<xsl:copy-of select = "Employee" />
</xsl:template>
回答2:
If you want to serialize a DOM node to a string use e.g.
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.Document;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
...
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
String str = writer.writeToString(node);
So returning a NodeList you could use
String expression = "/Employees/*";
System.out.println(expression);
NodeList elements = (NodeList)xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < elements.getLength(); i++)
{
System.out.println(writer.writeToString(element.item(i));
}
回答3:
First, if you want to match each Employee, ideally your XPath expression should be Employee not /Employees/*. If you know the tag name, you also don't need XPath, you can just do xmlDocument.getElementsByTagName("Employee").
If you want to serialize a node to a String, you can use an Transformer, something like this:
Transformer t = TransformerFactory.newTransformer();
NodeList nodes = xmlDocument.getElementsByTagName("Employee");
for(int i = 0; i < nodes.getLength(); i++) {
StringWriter sw = new StringWriter();
t.transform(new DOMSource(nodes.item(i)), new StreamResult(sw));
String serialized = sw.toString();
System.out.println(serialized);
}
来源:https://stackoverflow.com/questions/32905376/parsing-xml-to-get-all-child-nodes-and-their-data-using-java-xpath