6 Replies Latest reply: Oct 15, 2012 12:45 AM by 967752 RSS

    How get all child elements from XML

    967752
      Hi
      I have one xml i tried to parse that xml using dom parser and i need to get some child elements using java

      <Group>
      <NAME>ABC</NAME>
      <Age>24</AgeC>
      ---------some data here......
      <Group1>
      <group1Category>
      <NAME>ABCTest</NAME>
      <age>27</Age>
      ----Some data here
      <group1subcategory>
      <subcategory>
      <NAME>ABCDEF</NAME>
      <age>28</Age>



      my intention was
      get group name (here ABC) i need all other name value from group1category ,group1 subcategory but pblm that
      my xml contains any number of Group nodes...but only i want name contains ABC

      i wriiten code like this
      DocumentBuilderFactory factory = DocumentBuilderFactory
      .newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(xmlFile);
      NodeList nodeList = document.getElementsByTagName("*");

      for (int i = 0; i < nodeList.getLength(); i++)
      {
      Element element = (Element) nodeList.item(i);
      }
      what is next step i need to do..please help
        • 1. Re: How get all child elements from XML
          maheshguruswamy
          I suggest you use xpath to do this. You can use the starts with function to grab it. http://www.w3schools.com/xpath/xpath_functions.asp#string
          • 2. Re: How get all child elements from XML
            967752
            thanks..can u show an exmple.
            • 3. Re: How get all child elements from XML
              gimbal2
              Why would you ask anyone here to invest time into serving you an example on a platter when you can go out and Google for one yourself, spending only your own time?

              Research is a big chunk of your job description, I suggest you get used to it. Now when you actually have some code of your own that you need help with, feel free to drop in and ask for help.
              • 4. Re: How get all child elements from XML
                967752
                Sorry for inconvenience caused..i only asked if any ideas i not ask any body to spent time for me...
                This is simple code developed using xpath..i not know how i proceed further
                public class Demo {

                public static void main(String[] args) {
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                try {
                DocumentBuilder builder = domFactory.newDocumentBuilder();
                Document dDoc = builder.parse("hello.xml");

                XPath xpath = XPathFactory.newInstance().newXPath();



                javax.xml.xpath.XPathExpression expr = xpath.compile("//Group/NAME");

                Object Name= expr.evaluate(dDoc, XPathConstants.STRING);
                System.out.println(Name);


                } catch (Exception e) {
                e.printStackTrace();
                }

                i need get group name (here ABC) i need all other name value from group1category ,group1 subcategory but pblm that
                ..how i done in XPATH and also do manipulation of remining result...

                i also try with DOM like
                NodeList nodeList = document.getElementsByTagName("GROUP");
                for (int i = 0; i < nodeList.getLength(); i++)
                {


                Element element = (Element) nodeList.item(i);
                if (element.getNodeName().matches("ECUC-MODULE-DEF"))
                {
                String str=((Element) nodeList.item(i)).getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();

                if(str.equalsIgnoreCase("abc")){

                NodeList children = element.getChildNodes();
                for (int k = 0; k < children.getLength(); k++) {
                Node child = children.item(k);

                System.out.println("children"+children.getLength());
                if (child.getNodeType() != Node.TEXT_NODE) {
                if(child.getNodeName().equalsIgnoreCase("Group1"))
                {
                }
                how iterate for particular ABC name to group1 and subcategory
                • 5. Re: How get all child elements from XML
                  maheshguruswamy
                  964749 wrote:
                  Sorry for inconvenience caused..i only asked if any ideas i not ask any body to spent time for me...
                  This is simple code developed using xpath..i not know how i proceed further
                  public class Demo {

                  public static void main(String[] args) {
                  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                  try {
                  DocumentBuilder builder = domFactory.newDocumentBuilder();
                  Document dDoc = builder.parse("hello.xml");

                  XPath xpath = XPathFactory.newInstance().newXPath();



                  javax.xml.xpath.XPathExpression expr = xpath.compile("//Group/NAME");

                  Object Name= expr.evaluate(dDoc, XPathConstants.STRING);
                  System.out.println(Name);


                  } catch (Exception e) {
                  e.printStackTrace();
                  }

                  i need get group name (here ABC) i need all other name value from group1category ,group1 subcategory but pblm that
                  ..how i done in XPATH and also do manipulation of remining result...

                  i also try with DOM like
                  NodeList nodeList = document.getElementsByTagName("GROUP");
                  for (int i = 0; i < nodeList.getLength(); i++)
                  {


                  Element element = (Element) nodeList.item(i);
                  if (element.getNodeName().matches("ECUC-MODULE-DEF"))
                  {
                  String str=((Element) nodeList.item(i)).getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();

                  if(str.equalsIgnoreCase("abc")){

                  NodeList children = element.getChildNodes();
                  for (int k = 0; k < children.getLength(); k++) {
                  Node child = children.item(k);

                  System.out.println("children"+children.getLength());
                  if (child.getNodeType() != Node.TEXT_NODE) {
                  if(child.getNodeName().equalsIgnoreCase("Group1"))
                  {
                  }
                  how iterate for particular ABC name to group1 and subcategory
                  Few things

                  1. Use code tags to format code
                  2. Explain the problem statement clearly. Take time to formulate your question. Explain what you expect from your code and what you are getting along with any exceptions that are being thrown
                  • 6. Re: How get all child elements from XML
                    967752
                    i am trying to achieve those records match name ABC from GRO UPnode, retrieve child nodes names like ABCTEST(from <group1Category> ) and ABCDEF from (<group1subcategory>)
                    my requested output if i need to retrieve ABCTEST,ABCDEF if filtered ABC as name