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 7727635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:30:30+00:00 2026-06-01T05:30:30+00:00

Custom string format to xml help needed. Java and C#. [Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)] [Node(X)][CHILD0(Y)][OBJECT1(B)][Key1(1)][Key2(2)][Key3(3)] [Node(X)][CHILD0(Y)][OBJECT1(C)][Key1(4)] [Node(X)][CHILD0(Y)][OBJECT2(A)][Key1(1)][Key2(2)][Key3(3)]

  • 0

Custom string format to xml help needed. Java and C#.

[Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)]
[Node(X)][CHILD0(Y)][OBJECT1(B)][Key1(1)][Key2(2)][Key3(3)]
[Node(X)][CHILD0(Y)][OBJECT1(C)][Key1(4)]
[Node(X)][CHILD0(Y)][OBJECT2(A)][Key1(1)][Key2(2)][Key3(3)]
[Node(X)][CHILD0(Y)][OBJECT2(B)][Key1(4)][Key2(5)]
[Node(X)][CHILD1(Z)][OBJECT1(A)][Key1(7)][Key2(8)][Key3(9)]
[Node(X)][CHILD1(Z)][OBJECT2(A)][Key1(15)][Key2(18)]

There may be ‘n’ number of string rows like the above samples.
What is the best method to serialize this to an xml file as below

If the below shown xml is also incorrect, then please advise the correct format as well.
I tried serializing using some examples provided in stackoverflow and code project, but I’m unable
to get the xml in the below format. Trying to do it in java and c#. Thank you all in advance.

[X]
    [Y]  
        [OBJECT1]
            [A]
                <Key1>1</Key1>
                <Key2>2</Key2>
            [/A]
            [B]
            <Key1>2</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
        [/B]
        [C]
            <Key1>4</Key1>
        [/C]
    [/OBJECT1]
    [OBJECT2]
        [A]
            <Key1>1</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
        [/A]
        [B]
            <Key1>4</Key1>
            <Key2>5</Key2>
        [/B]
    [/OBJECT2]
    [/Y]
    [Z]
        [OBJECT1]
        [A]
            <Key1>7</Key1>
            <Key2>8</Key2>
            <Key2>9</Key2>
            [/A]
        [/OBJECT1]
        [OBJECT2]
        [A]
            <Key1>15</Key1>
            <Key2>18</Key2>
        [/A]
        [/OBJECT2]
    [/Z]
[/X]
  • 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-06-01T05:30:31+00:00Added an answer on June 1, 2026 at 5:30 am

    Here’s a parser class for your purpose:

    public class XmlSerializer {
    
    private String input;
    private Element currentNode, currentChild, currentObject;
    private Map<String, Element> nodes;
    private Map<Element, Map<String, Element>> children, objects;
    
    /**
     * Initializes the serializer with the given input string.
     * @param  input input string
     */
    public XmlSerializer(String input) {
        this.input = input;
        this.nodes = new HashMap<String, Element>();
        this.children = new HashMap<Element, Map<String,Element>>();
        this.objects = new HashMap<Element, Map<String,Element>>();
    }
    
    /**
     * Parses the input string and returns the XML document.
     * @return XML document
     */
    public Document parseDocument()
    throws ParserConfigurationException {
        Pattern pattern = Pattern.compile("\\[(.+?)\\((.+?)\\)\\]");
        Matcher matcher = pattern.matcher(input);
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element root = dom.createElement("root");
        dom.appendChild(root);
    
        while (matcher.find()) {
            String key = matcher.group(1);
            String value = matcher.group(2);
    
            if ("Node".equals(key)) {
                currentNode = parseElement(key, value, dom, root, nodes, children);
            } else if (currentNode != null && key.startsWith("CHILD")) {
                currentChild = parseElement(key, value, dom, currentNode,
                        children.get(currentNode), objects);
            } else if (currentChild != null && key.startsWith("OBJECT")) {
                currentObject = parseElement(key, value, dom, currentChild,
                        objects.get(currentChild), null);
            } else {
                Element property = parseProperty(key, value, dom);
                currentObject.appendChild(property);
            }
        }
    
        return dom;
    }
    
    /**
     * Returns the parsed XML document as string.
     * @return XML document as string
     */
    public String toXML()
    throws TransformerFactoryConfigurationError, ParserConfigurationException, TransformerException {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source source = new DOMSource(parseDocument());
        Result result = new StreamResult(writer);
        transformer.transform(source, result);
        return writer.getBuffer().toString();
    }
    
    /**
     * Parses an element.
     * @param  key      key in {@code [key(value)]} string
     * @param  value    value in {@code [key(value)]} string
     * @param  dom      DOM
     * @param  parent   parent element to add this element to
     * @param  cache    cache for the parsed element
     * @param  subCache sub-cache to initialize (optional)
     * @return the element
     */
    private Element parseElement(String key, String value, Document dom, Element parent,
            Map<String, Element> cache, Map<Element, Map<String, Element>> subCache) {
        Element el = cache.get(value);
        if (el == null) {
            el = dom.createElement(value);
            cache.put(key, el);
            parent.appendChild(el);
            if (subCache != null)
                subCache.put(el, new HashMap<String, Element>());
        }
        return el;
    }
    
    /**
     * Parses a property element.
     * @param  key   key in {@code [key(value)]} string
     * @param  value value in {@code [key(value)]} string
     * @param  dom   DOM
     * @return the element
     */
    private Element parseProperty(String key, String value, Document dom) {
        Element property = dom.createElement(key);
        property.setTextContent(value);
        return property;
    }
    
    }
    

    Use it like this:

    String input;  // [Node(X)][CHILD0(Y)][OBJECT1(A)][Key1(1)][Key2(2)]...
    String xml = new XmlSerializer(input).toXML();
    System.out.println(xml);
    

    Output:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <root>
      <X>
        <Y>
          <A>
            <Key1>1</Key1>
            <Key2>2</Key2>
          </A>
        </Y>
        <Y>
          <B>
            <Key1>1</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
          </B>
        </Y>
      </X>
      <X>
        <Y>
          <C>
            <Key1>4</Key1>
          </C>
        </Y>
      </X>
      <X>
        <Y>
          <A>
            <Key1>1</Key1>
            <Key2>2</Key2>
            <Key3>3</Key3>
          </A>
        </Y>
      </X>
      <X>
        <Y>
          <B>
             <Key1>4</Key1>
             <Key2>5</Key2>
          </B>
        </Y>
      </X>
      <X>
        <Z>
          <A>
            <Key1>7</Key1>
            <Key2>8</Key2>
            <Key3>9</Key3>
          </A>
        </Z>
      </X>
      <X>
        <Z>
          <A>
            <Key1>15</Key1>
            <Key2>18</Key2>
          </A>
        </Z>
      </X>
    </root>
    

    Go from here and optimize it a little, for example if you don’t want repeated <X>...</X> nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm want to parse a custom string format that is persisting an object graphs
I'm calling .NET's DateTime.ParseExact with a custom format string along the lines of MM/dd/yyyy
Is there any way I can specify a standard or custom numeric format string
I'm writing a game in Android that uses a custom XML format to describe
I was trying to create an IFormatProvider implementation that would recognize custom format strings
Is there a way to search in a database a custom string and replace
Is including a application package prefix while defining a custom action string is convention
The string passed to my custom function is the following: SELECT key FROM ubis
My custom method to get json from string: function GetJSON(a) { if (typeof a
Made a custom obj called Item with some string fields and one float. .h

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.