If not understand let me know will explain.
public static Document createDoc() throws Exception {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder= factory.newDocumentBuilder();
return builder.newDocument();
}
public static void loadXml() throws Exception {
//region Accessing xml document parsing it into document through document builder
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder= factory.newDocumentBuilder();
File file=new File("hamlet.xml");
Document doc= builder.parse(file);
//endregion
XPathFactory xPathFactory=XPathFactory.newInstance();
XPath path= xPathFactory.newXPath();
//region Checking if persona exists in doc
System.out.print("Enter name of Persona to be checked if is in the play 'HAMLET': ");
String name= createScanner().next();
name=name.toUpperCase(Locale.ROOT);
// XPathExpression xPathExpression=path.compile("//SPEECH[SPEAKER='"+name+"']/LINE");
XPathExpression xPathExpression=path.compile("//Speech[Speaker='"+name+"']/Line");
Element root=doc.getDocumentElement();
NodeList persona= root.getElementsByTagName("PERSONA");
boolean found=false;
Document lines= createDoc();
Element linesofspeaker=lines.createElement("linespeaker"); //add all acts here
linesofspeaker.setAttribute("id",name);
lines.appendChild(linesofspeaker); //append child of doc node - root node
while(!found){
for(int i=0;i<persona.getLength();i++)
{
Element persona1 = (Element) persona.item(i);
if(persona1.getTextContent().split(",")[0].toUpperCase(Locale.ROOT).equals(name))
{
System.out.println(name+" is valid !");
found=true;
break;
}
}
if(!found)
{ System.out.println(name+ "Is Not Valid !");
System.out.print("Enter name of Persona to be checked if is in the play 'HAMLET' again : ");
name= createScanner().next();
name=name.toUpperCase(Locale.ROOT);
}
}
//endregion
//Output xml results
NodeList nodes=(NodeList) xPathExpression.evaluate(doc,XPathConstants.NODESET);
System.out.println("No of lines -> "+nodes.getLength());
//NodeList acts=(NodeList) root.getElementsByTagName("ACT");
XPathExpression actsofName= path.compile("//ACT[SCENE//SPEAKER='"+ name +"']");
// XPathExpression nameofact=path.compile("./ACT/TITLE");
NodeList acts=(NodeList) actsofName.evaluate(doc,XPathConstants.NODESET);
System.out.println("No of acts in which speaker speaks -> " + acts.getLength());
if(acts.getLength()>0)
{
for(int i=0;i< acts.getLength();i++)
{
Element act = (Element) acts.item(i);
XPathExpression nameofact = path.compile("./TITLE"); //get title node
Node nameofacc = (Node) nameofact.evaluate(act, XPathConstants.NODESET);
//!
String actname = nameofacc.getTextContent();
Element newact= lines.createElement("ACT");
newact.setAttribute("ID",actname);
// newact.setTextContent(actname);
linesofspeaker.appendChild(newact);
//XmlUtils.documentToString(lines);
saveDoc(lines,"output.xml");
//SCENE[SPEECH/SPEAKER="BERNARDO"
XPathExpression scenes=path.compile(".//SCENE[SPEECH/SPEAKER= '"+name+"']");
NodeList scenesofname= (NodeList) scenes.evaluate(act,XPathConstants.NODESET);
int noscenes=scenesofname.getLength();
//RESUME!
for(int j=0;j<scenesofname.getLength();j++)
{
//
Element Scene=lines.createElement("SCENE");
Element curScene=(Element) scenesofname.item(j);
Element scenetitle= (Element) scenesofname.item(j).getFirstChild();
String scenename=scenetitle.getTextContent();
Scene.setAttribute("title",scenename);
newact.appendChild(Scene);
XPathExpression speeches=path.compile(".//SPEECH[SPEAKER='"+name+"']");
NodeList speechlist=(NodeList) speeches.evaluate(curScene,XPathConstants.NODESET);
for (int k=0;k<speechlist.getLength();k++)
{
Element speech=(Element) speechlist.item(k);
//SPEECH[SPEAKER='BERNARDO']
XPathExpression getlines=path.compile(".//LINE");
NodeList linez = (NodeList) getlines.evaluate(speech,XPathConstants.NODESET);
Element newspeech=lines.createElement("SPEECH");
newspeech.setAttribute("id", String.valueOf(k+1));
for (int l=0;l<linez.getLength();l++){
Element newline=(Element) lines.createElement("LINE");
newline.setTextContent(linez.item(l).getTextContent());
newspeech.appendChild(newline);
}
Scene.appendChild(newspeech);
}
//Scene.appendChild(lines.createElement("SPEECH"));
}
// scenesofname.item(i);
//linesofspeaker.appendChild(Scene);
System.out.println("No of Scenes in act " + (i+1) +" where "+ name+" speaks -->" +scenesofname.getLength());
saveDoc(lines,name+".xml");
}
}
}