import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.lang.String;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class twigstack {
public static void main(String argv[]) {
try {
File fXmlFile = new File("d:\\twig.xml");
//String filepath = "d:\\myproduct1.xml";
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Element root=doc.getDocumentElement();
//Element ar[];
//Queue q=new Queue();
//Queue q1=new Queue();
Node[] ta=new Node[0];
Node[] tf=new Node[0];
Node[] tl=new Node[2];
Node[] tj=new Node[1];
Node[] td=new Node[1];
t1 t=new t1();
t2 tt=new t2();
String st="//author[/fn/jane]/ln/doe";
String sub=st.substring(2,8);
String subf=st.substring(10,12);
String subj=st.substring(13,17);
String subl=st.substring(19, 21);
String subd=st.substring(22, 25);
//System.out.println(subf);
//System.out.println(sub);
if(root.hasChildNodes())
{
ta=t.mymethod(root,sub);
tf=t.mymethod(root, subf);
tl=t.mymethod(root, subl);
}
int n=ta.length;
System.out.println(n);
for (int i=0;i<n;i++)
{
Node l=ta[i];
System.out.println(l.getNodeName());
Element l1=(Element)l;
System.out.println(l1.getAttribute("start"));
//System.out.println("1");
}
tf=t.mymethod(root, subf);
//System.out.println(tf[2].getNodeName());
int n1=tf.length;
//if(tf.)
for (int j=0;j<n1;j++)
{
System.out.println(tf[j].getNodeName());
Element el1=(Element)tf[j];
//System.out.println("1");
System.out.println(el1.getAttribute("start"));
//System.out.println("1");
}
/*for (int j=0;j<tl.length;j++)
{
System.out.println(tl[j].getNodeName());
Element el1=(Element)tl[j];
//System.out.println("1");
System.out.println(el1.getAttribute("start"));
//System.out.println("1");
}*/
//System.out.println("1");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//Another class:
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class t1 {
Node[] mymethod (Node e,String sub)
{
//Element ele=(Element)e;
Node[] copy=new Node[3];
int j=0;
NodeList n=e.getChildNodes();
for(int i=0;i<n.getLength();i++)
{
Node f=n.item(i);
if (f.getNodeType() == Node.ELEMENT_NODE)
{
//x++;
//Element el = (Element) f;
//System.out.println(f.getNodeName());
String s=f.getNodeName();
//System.out.println(sub);
if(s.equalsIgnoreCase(sub))
{
copy[j]=f;
Node k=copy[j];
Element l =(Element)k;
//System.out.println(l.getNodeName());
j++;
//System.out.println(s);
}
if (f.hasChildNodes())
//x=el.getAttribute("start");
mymethod(f,sub);
else
//System.out.println(" child of"+r+":"+p+":"+s1);
continue;
//x=ele.getAttribute("start");
}
}
for (int i=0;i<j;i++)
{
System.out.println(copy[i].getNodeName());
}
//q.display();
return copy;
//x1--;
//}
}
}
//twig.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <allauthors end="16" level="0" start="1">
- <author end="6" level="1" start="2">
- <fn end="4" level="2" start="3">
<jane end="4" level="3" start="4" />
</fn>
- <ln end="6" level="2" start="5">
<poe end="6" level="3" start="6" />
</ln>
</author>
- <author end="11" level="1" start="7">
- <fn end="9" level="2" start="8">
<john end="9" level="3" start="9" />
</fn>
- <ln end="11" level="2" start="10">
<doe end="11" level="3" start="11" />
</ln>
</author>
- <author end="16" level="1" start="12">
- <fn end="14" level="2" start="13">
<jane end="14" level="3" start="14" />
</fn>
- <ln end="16" level="2" start="15">
<doe end="16" level="3" start="16" />
</ln>
</author>
</allauthors>