Sunday, 1 February 2015

Attempt to get next and previous sibling of a node after parsing xml for a specific node using java



I have an xml containing the log of 1000 events where each one has a key, My objective is when a user search for an event key, i should display him the xml of the event node having this key and prepare the info of the previous and next sibling.


The xml structure is as follow:



<LOG>
<EVENT_SET>
<DOCGET system="T610_00" fingerPrint="NO_SIGNATURE">
<event>
<key>382</key>
<date>2015-01-28T09:15:15.350+0000</date>
<service>CORE</service>
<class>APPLICATION</class>
</event>
<document>
<docuri>getdocs:///DocMapCSDOCS.dPortal/1</docuri>
<sign_info>
<signature>VqtR9Gpny/MPE43/5o4hJXp8bR7gbsVUJqHlTI+VfztMSQecTpZwAQpxmorrdBJKvmn+h7eZzV1geVodkVECvOjQMRmRbnpT6mrpbiXxjDOsZsQRDNemTYUKETrQFIBRtXcjoP61une1LOsS5C749ehwbZ1jEaNH6fPjH4n+OH4=</signature>
</sign_info>
</document>
</DOCGET>
<DOCGET system="T610_00" fingerPrint="NO_SIGNATURE">
<event>
<key>383</key>
<date>2015-01-28T09:15:18.310+0000</date>
<service>CORE</service>
<class>APPLICATION</class>
</event>
<document>
<docuri>getdocs:///DocMapCSDOCS.dPortal/2</docuri>
<sign_info>
<signature>VqtR9Gpny/MPE43/5o4hJXp8bR7gbsVUJqHlTI+VfztMSQecTpZwAQpxmorrdBJKvmn+h7eZzV1geVodkVECvOjQMRmRbnpT6mrpbiXxjDOsZsQRDNemTYUKETrQFIBRtXcjoP61une1LOsS5C749ehwbZ1jEaNH6fPjH4n+OH4=</signature>
</sign_info>
</document>
</DOCGET>
.......
</EVENT_SET>
</LOG>


And my code till now to get the node of a key is:



DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
//parsing the xml file
journaldoc = builder.parse(journalFile);
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();

//xpath to search for the event having the key requated by the user
String sXpath = "//DOCGET[event/key='"+eventkey+"']";
XPathExpression expr = xpath.compile(sXpath);
Object result = expr.evaluate(journaldoc, XPathConstants.NODE);
Node eventnode = (Node) result;

//return to the user the xml part with root DOCGET, and having the key requested
DOMImplementationLS domImplementationLS = (DOMImplementationLS) document.getImplementation();
LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
String xmlNode = lsSerializer.writeToString(document);


Now i need to get the previous and next sibling of this node and get their keys in order to save them in a HashMap.


The problem is when doing eventnode.getPreviousSibling and eventnode.getNextSibling , i am getting corrupted info and i am not able to get the key of these events.


Does anyone has a better idea to do this?


Thank you, Karine


No comments:

Post a Comment