public static ReaderLog GetLastElements(){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("sheet.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOftagdata = doc.getElementsByTagName("tagdata");
int totaltagdata = listOftagdata.getLength();
System.out.println("Total no of tagdata : " + totaltagdata);
for(int s=listOftagdata.getLength()-1 ; s> listOftagdata.getLength()-2 ; s--){
Node node = listOftagdata.item(s);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element)node;
ReaderLog r = new ReaderLog() ;
//-------
NodeList nodelist = element.getElementsByTagName("tagid");
Element element1 = (Element) nodelist.item(0);
NodeList ntagid = element1.getChildNodes();
r.TagId = (ntagid.item(0)).getNodeValue() ;
//-------
NodeList nodelist2 = element.getElementsByTagName("date");
Element element2 = (Element) nodelist2.item(0);
NodeList ntagid2 = element2.getChildNodes();
r.Date = ( (ntagid2.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist3 = element.getElementsByTagName("time");
Element element3 = (Element) nodelist3.item(0);
NodeList ntagid3 = element3.getChildNodes();
r.Time = ( (ntagid3.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist4 = element.getElementsByTagName("antno");
Element element4 = (Element) nodelist4.item(0);
NodeList ntagid4 = element4.getChildNodes();
r.AntNo = ( (ntagid4.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist5 = element.getElementsByTagName("data");
Element element5 = (Element) nodelist5.item(0);
NodeList ntagid5 = element5.getChildNodes();
r.Data = ( (ntagid5.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist6 = element.getElementsByTagName("readerip");
Element element6 = (Element) nodelist6.item(0);
NodeList ntagid6 = element6.getChildNodes();
r.ReaderIP = ( (ntagid6.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist7 = element.getElementsByTagName("computerdate");
Element element7 = (Element) nodelist7.item(0);
NodeList ntagid7 = element7.getChildNodes();
System.out.println( (ntagid7.item(0)).getNodeValue() ) ;
//-------
NodeList nodelist8 = element.getElementsByTagName("computertime");
Element element8 = (Element) nodelist8.item(0);
NodeList ntagid8 = element8.getChildNodes();
r.ComputerTime = ( (ntagid8.item(0)).getNodeValue() ) ;
return r ;
}//end of if clause
}//end of for loop with s var
} catch (SAXException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
return null ;
}
public static void RemoveLastElement() throws TransformerException, SAXException, IOException{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new File("sheet.xml"));
DocumentTraversal traversal = (DocumentTraversal) doc;
Node a = doc.getDocumentElement();
NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
Element b = null;
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if ("tagdata".equals(e.getTagName())) {
b = e;
} else if ("tagdata".equals(e.getTagName()) && /*"13".equals(e.getTextContent()) && */ b != null) {
}
}
a.removeChild(b);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("sheet.xml"));
transformer.transform(source, result);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void AddElemet(ReaderLog r) throws TransformerException{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("sheet.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
Element newB = doc.createElement("tagdata");
Element newC = doc.createElement("tagid");
newC.setTextContent( r.TagId );
Element newD = doc.createElement("date");
newD.setTextContent( r.Date );
Element newE = doc.createElement("time");
newE.setTextContent( r.Time );
Element newF = doc.createElement("antno");
newF.setTextContent( r.AntNo );
Element newG = doc.createElement("data");
newG.setTextContent( r.Data );
Element newH = doc.createElement("readerip");
newH.setTextContent( r.ReaderIP );
Element newI = doc.createElement("computerdate");
newI.setTextContent( r.ComputerDate.toString() );
Element newJ = doc.createElement("computertime");
newJ.setTextContent( r.ComputerDate.toString() );
newB.appendChild(newC);
newB.appendChild(newD);
newB.appendChild(newE);
newB.appendChild(newF);
newB.appendChild(newG);
newB.appendChild(newH);
newB.appendChild(newI);
newB.appendChild(newJ);
doc.getDocumentElement().appendChild(newB);
doc.normalize();
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("sheet.xml"));
transformer.transform(source, result);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SAXException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex);
}
}
No comments:
Post a Comment