SAXParser
using SAX parser can i get value only for status.EmploymentStatus.StatusDesc ? below is my xml.
<Status>
<EmploymentStatus>
<StatusDesc>Active</StatusDesc>
<StartDt>19530618</StartDt>
</EmploymentStatus>
<TaxStatus>
<StatusDesc>Independent Contractor</StatusDesc>
<StartDt>19540617</StartDt>
</TaxStatus>
<Position>
<PositionCd>000051</PositionCd>
<StartDt>20000322</StartDt>
<StatusDesc>IC FINANCIAL ADVISOR</StatusDesc>
</Position>
<FACTSRole>
<FACTSRoleCd>40</FACTSRoleCd>
<StatusDesc>Fas Qualified</StatusDesc>
</FACTSRole>
</Status>
Basically, SAXParser will handle data extraction for all nodes. As I answered in your previous post about using arraylist, I'll handle it like this.
public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
if (needData) { // needData is a boolean
if (chkNodes(qName)) { // check if the node is in the arraylist
needData = true; // if yes, then you continue to get the data
}
else
needData = false; // if no, just skip it
}
}
public boolean chkNodes(String qName) {
for(int i=0; i<arrayList.size(); i++) {
if (qName.equals(arrayList.get(i)))
return true;
}
return false;
}
>
Hi
I guess you are better off using DOM + Xpath rather than SAX...The following sample using JAXP in JDK 5.0
Here is an example of how u can do this..
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse("D:\\Development\\eibus\\mntc\\study\\xml\\xpath\\inputp.xml");
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
Node content = (Node)xpath.evaluate("//Acknowledgement//UUId", dom, XPathConstants.NODE);
System.out.println(((Element)content).getTextContent());
((Element)content).setTextContent("newvalue");
System.out.println(((Element)content).getTextContent());