Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7045431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:31:47+00:00 2026-05-28T02:31:47+00:00

I have a XML like this: <group name=A> <element>1</element> <groups> <group name=B> <element>2</element> <element>3</element>

  • 0

I have a XML like this:

<group name="A">
  <element>1</element>
  <groups>
    <group name="B">
      <element>2</element>
      <element>3</element>
      <groups></groups>
    </group>
    <group name="C">
      <element>4</element>
      <groups></groups>
    </group>
  </groups>
</group>

Is it possible to recover and treat entire XML using Java and VTD-XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:31:48+00:00Added an answer on May 28, 2026 at 2:31 am

    Here’s full code:

    import java.util.Deque;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    
    import org.junit.Test;
    
    import com.ximpleware.AutoPilot;
    import com.ximpleware.VTDGen;
    import com.ximpleware.VTDNav;
    
    public class RecursiveVtdTest {
      public static class Group {
        // attribute "name"
        private String name;
    
        // elements "element"
        private List<Element> elements = new LinkedList<Element>();
    
        // elements "group"
        private List<Group> groups = new LinkedList<Group>();
    
        public String toString(int ind) {
          String str = "";
          for (int i=0; i<ind; i++) {
            str += " ";
          }
          str += String.format("name=%s, elements=%s, groups=\n", this.name, this.elements.toString());
          for (Group g: this.groups) {
            str += g.toString(ind + 2);
          }
          return str;
        }
    
        public String getName() {
          return this.name;
        }
    
        public void setName(String name) {
          this.name = name;
        }
    
        public List<Element> getElements() {
          return this.elements;
        }
    
        public List<Group> getGroups() {
          return this.groups;
        }
      }
    
      public static class Element {
        // text of XML element
        private String value;
    
        @Override
        public String toString() {
          return String.format("v=%s", this.value);
        }
    
        public Element(String value) {
          this.value = value;
        }
    
        public String getValue() {
          return this.value;
        }
    
        public void setValue(String value) {
          this.value = value;
        }
      }
    
      @Test
      public void loadRecursiveXML() throws Exception {
        String xml = "" +
            "<group name=\"A\">\n" + 
            "  <element>1</element>\n" + 
            "  <groups>\n" + 
            "    <group name=\"B\">\n" + 
            "      <element>2</element>\n" + 
            "      <element>3</element>\n" + 
            "      <groups></groups>\n" + 
            "    </group>\n" + 
            "    <group name=\"C\">\n" + 
            "      <element>4</element>\n" + 
            "      <groups></groups>\n" + 
            "    </group>\n" + 
            "  </groups>\n" + 
            "</group>\n";
    
        byte[] doc = xml.getBytes();
    
        VTDGen vtd = new VTDGen();
        vtd.setDoc(doc);
    
        vtd.parse(true);
    
        VTDNav nav = vtd.getNav();
        AutoPilot ap = new AutoPilot(nav);
        ap.selectElementNS("*", "*");
    
        Group currentGroup = null;
        Deque<List<Group>> groupLists = new LinkedList<List<Group>>();
        groupLists.push(new LinkedList<Group>());
    
        int currentDepth = -1;
    
        while (ap.iterate()) {
          int n = nav.getCurrentIndex();
          int d = nav.getCurrentDepth();
          String elName = nav.toString(n);
          if (elName.equals("group")) {
            currentGroup = new Group();
            // group name
            int type = nav.getTokenType(n + 1);
            if (type == VTDNav.TOKEN_ATTR_NAME && nav.toString(n + 1).equals("name")) {
              String name = nav.toString(n + 2);
              currentGroup.setName(name);
            }
            // remove group lists if navigator went up in XML
            if (currentDepth >= d) {
              for (int i = 0; i <= currentDepth - d; i+=2) {
                groupLists.pop();
              }
            }
            // add to current group list
            groupLists.peek().add(currentGroup);
            // change current group list
            groupLists.push(currentGroup.getGroups());
            currentDepth = d;
          } else if (elName.equals("element")) {
            currentGroup.getElements().add(new Element(nav.toString(n + 1)));
          }
        }
    
        System.out.println(groupLists.getLast().get(0).toString(0));
      }
    }
    

    And result:

    name=A, elements=[v=1], groups=
      name=B, elements=[v=2, v=3], groups=
      name=C, elements=[v=4], groups=
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have some xml like this: <Data> <Rows> <Row> <Field Name=title>Mr</Field> <Field Name=surname>Doe</Field> <Row>
I have a XML like this: <StateTree> <State ID=01> <Name>State1</Name> <CityList> <City ID=01 Order=1
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I have an XML like this: <EXP> <TITLES> <SUBTITLE CL=AXT4 FL=1 NB=Text 1/> </TITLES>
hi i have an xml like this <?xml version=1.0?> <DataSetExchangeWMS xmlns=http://tempuri.org/DataSetExchangeWMS.xsd> <dtObjektInfo> <LanguageCode>1031</LanguageCode> <LiegenschaftID>7463</LiegenschaftID>
If I have some xml like this: <mynode> <mysubnode> <mysubsubnode>hello world</mysubsubnode> some more text
Let's say I have some XML like this <channel> <item> <title>This is title 1</title>
Greetings! I have some XML like this: <Root> <AlphaSection> . . . </AlphaSection> <BetaSection>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.