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);
}
}
Tuesday, June 26, 2012
Sunday, June 24, 2012
Trim String by String And PadLeft Functions in java
public static String trimStringByString(String text, String trimBy) {
int beginIndex = 0;
int endIndex = text.length();
while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
beginIndex += trimBy.length();
}
while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
endIndex -= trimBy.length();
}
return text.substring(0, endIndex);
}
public static String padLeft(String in, int size, char padChar) {
if (in.length() <= size) {
char[] temp = new char[size];
/* Llenado Array con el padChar*/
for(int i =0;i<size;i++){
temp[i]= padChar;
}
int posIniTemp = size-in.length();
for(int i=0;i<in.length();i++){
temp[posIniTemp]=in.charAt(i);
posIniTemp++;
}
return new String(temp);
}
return "";
}
int beginIndex = 0;
int endIndex = text.length();
while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
beginIndex += trimBy.length();
}
while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
endIndex -= trimBy.length();
}
return text.substring(0, endIndex);
}
public static String padLeft(String in, int size, char padChar) {
if (in.length() <= size) {
char[] temp = new char[size];
/* Llenado Array con el padChar*/
for(int i =0;i<size;i++){
temp[i]= padChar;
}
int posIniTemp = size-in.length();
for(int i=0;i<in.length();i++){
temp[posIniTemp]=in.charAt(i);
posIniTemp++;
}
return new String(temp);
}
return "";
}
Monday, June 11, 2012
tsql program to kill connection sleep more than 10 min can be run in sp agent
exec sp_who
go
DECLARE @v_spid INT
DECLARE c_Users CURSOR
FAST_FORWARD FOR
SELECT SPID
FROM master..sysprocesses (NOLOCK)
WHERE spid>50
AND status='sleeping'
AND DATEDIFF(mi,last_batch,GETDATE())>=10
AND spid<>@@spid
OPEN c_Users
FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT 'KILLing '+CONVERT(VARCHAR,@v_spid)+'...'
EXEC('KILL '+@v_spid)
FETCH NEXT FROM c_Users INTO @v_spid
END
CLOSE c_Users
DEALLOCATE c_Users
see also
http://support.microsoft.com/kb/137983
go
DECLARE @v_spid INT
DECLARE c_Users CURSOR
FAST_FORWARD FOR
SELECT SPID
FROM master..sysprocesses (NOLOCK)
WHERE spid>50
AND status='sleeping'
AND DATEDIFF(mi,last_batch,GETDATE())>=10
AND spid<>@@spid
OPEN c_Users
FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS=0)
BEGIN
PRINT 'KILLing '+CONVERT(VARCHAR,@v_spid)+'...'
EXEC('KILL '+@v_spid)
FETCH NEXT FROM c_Users INTO @v_spid
END
CLOSE c_Users
DEALLOCATE c_Users
see also
http://support.microsoft.com/kb/137983
Subscribe to:
Posts (Atom)