Reputation: 17883
I am working on XML to java parsing. I have a scenario similar to this.
I need to parse XML into java based on the attribute value.
I need to parse all EMPLOYEE elements with attribute PERMANENT="Y"
in one member and with attribute PERMANENT="N"
in another member.
<EMPLOYEE PERMANENT="Y">
<DETAILS NAME="AA" ID="1" AGE="28" />
<DETAILS NAME="BB" ID="2" AGE="29" />
</EMPLOYEE>
<EMPLOYEE PERMANENT="N">
<DETAILS NAME="CC" ID="3" AGE="28" />
<DETAILS NAME="DD" ID="4" AGE="29" />
</EMPLOYEE>
Java
public class Employee
{
// @XStreamAlias("EMPLOYEE") and attribute PERMANENT="Y"
private Details permanentEmployee;
// @XStreamAlias("EMPLOYEE") and attribute PERMANENT="N"
private Details tempEmployee;
}
I am not sure how to do this.
Can some one help me.
Upvotes: 1
Views: 129
Reputation: 2012
You can do it using the using the java xml api
The XPath expression to select the "DETAILS" nodes will look something like this:
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
//...
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/documentBody/EMPLOYEE[@PERMANENT="Y"]/DETAILS"
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
This returns a nodeList that you need to then iterate through for each DETAILS node, which I would assume describes one employee.
Upvotes: 1