XMLUtil { public static int getPosition(Element e) { Element parent = (Element)e.getParentNode(); List<Element> children = getChildElements(parent); return children.indexOf(e) + 1; } private  XMLUtil(); static Document parseXML(String xmlAsString); static Document parseXML(InputStream in); static void writeXML(Node xml, File file); static void writeXML(Node xml, OutputStream outputStream); static void removeNodes(Element parent, NodeList elements); static List<Element> getChildElementsByName(Element element,
			String localName); static List<Element> getChildElements(Element element); static void addAsFirstChild(Element element, Element newChild); static QName getQName(Node n); static void removeAllSubNodesExceptAttributes(Node n); static Document createDocument(); static String getContentsOfTextOnlyNode(Node n); static String getXPathForElement(Node e, NamespaceContext ctx); static int getPosition(Element e); static Text appendTextNode(Element e, String contents); static String getTextContent(Node e); static QName resolveQName(String qNameWithPrefix, Element element); static String resolveNamespacePrefix(String prefix, Element element); static QName resolveQNameTargetNS(String sName, Node context, String targetNS);  }
@Test public void testGetPosition() throws Exception { Document xml = XMLUtil.parseXML(getClass().getResourceAsStream("Position.xml")); List<Element> children = XMLUtil.getChildElements(xml.getDocumentElement()); assertEquals(4, children.size()); assertEquals(1, XMLUtil.getPosition(children.get(0))); assertEquals(2, XMLUtil.getPosition(children.get(1))); assertEquals(3, XMLUtil.getPosition(children.get(2))); assertEquals(4, XMLUtil.getPosition(children.get(3))); }