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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:44:39+00:00 2026-06-05T11:44:39+00:00

I was thinking to write a method in toString() like fashion so that it

  • 0

I was thinking to write a method in toString() like fashion so that it return XML representation of the class instance.

First I was thinking to write it like

public Element toElement() {
    // create Element instance and fill it
}

But I was unable to create empty Element instance inside, since Element creation requires Document instance to call it’s createElement().

So I rewrote method to

public Element toElement(Document doc) {
   Element ans = doc.createElement("myclasstag");

   // filling ans

   return ans;
}

But then I got runtime exception HIERARCHY_REQUEST_ERR since one can’t fill Element instance until it is attached to parent hierarchy.

So I was to rewrite method as follows

public Element toElement(Document doc, Element parent) {

   Element ans = doc.createElement("myclasstag");
   parent.appendChild(ans);

   // filling ans

   return ans;
}

But this way I need not return ans since it is already attached where it should be, so it became

public void append(Document doc, Element parent) {

   Element ans = doc.createElement("myclasstag");
   parent.appendChild(ans);

   // filling ans
}

which is now absolutely dislike toString().

Is it possible to create XML instance from down to up fashion like toString() does?

  • 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-05T11:44:41+00:00Added an answer on June 5, 2026 at 11:44 am

    Using XStream, I could do this:

    package com.adarshr;
    
    import com.thoughtworks.xstream.XStream;
    
    
    class Parent {
        private String name;
        private int age;
    
        public Parent(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    
    public class Test {
        private Parent parent = new Parent("Abcd", 30);
    
        public static void main(String[] args) throws Exception {
            System.out.println(new Test());
        }
    
        @Override
        public String toString() {
            return new XStream().toXML(this);
        }
    }
    

    Which prints:

    <com.adarshr.Test>
      <parent>
        <name>Abcd</name>
        <age>30</age>
      </parent>
    </com.adarshr.Test>
    

    Of course, it is fully customisable.

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

Sidebar

Related Questions

I want to write a class method that returns a value, if the class
I would like to write a C# method that would transform any title into
Imagine, I want to write a useless method called: isInstanceof that returns a boolean
Quick one; am I right in thinking that passing a string to a method
I am trying to write a method that loops and repeatedly calls other methods.
The DataContext.GetTable() method will return an object of type: System.Data.Linq.Table By doing that, I
I need to write a simple method that gets a string as an input,
I have noticed in Rails console that Model.search_for_ids.size != Model.all.size Moreover Thinking Shpinx method
OK, I know what you're thinking, "why write a method you do not want
I am trying to write a method that will determine the closest date chronlogically,

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.