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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:18:26+00:00 2026-05-28T17:18:26+00:00

I have to turn a org.w3c.dom.Document into a java.lang.String. I have found two possible

  • 0

I have to turn a org.w3c.dom.Document into a java.lang.String. I have found two possible approaches, one using org.w3c.dom.ls.LSSerializer and the other using a javax.xml.transform.Transformer. I have samples of each below.

Can anyone tell me which method is to be preferred?

public String docToStringUsingLSSerializer(org.w3c.dom.Document doc) {
    DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
    LSSerializer serializer = impl.createLSSerializer();
    return serializer.writeToString(doc);
}

public String docToStringUsingTransformer(org.w3c.dom.Document doc) {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter stw = new StringWriter();  
    transformer.transform(new DOMSource(doc), new StreamResult(stw));  
    return stw.toString();
}
  • 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-28T17:18:27+00:00Added an answer on May 28, 2026 at 5:18 pm

    There are several points to consider:

    1. LSSerializer is usually considered faster than Transformer.
    2. Nevertheless it heavily depends on the implementation. A Transformer based on SAX will have good performance. And there are different implementors (Xalan, Xerces, …).
    3. It is very easy to check which is better in your system. Design a simple test case with a complex XML. Run it in a loop thousdns of time, wrap that with time check (Syste.getCurrentMilliseconds or something) and you’ve got yourself an answer.
    4. Other nice answers include:
      • Is there a more elegant way to convert an XML Document to a String in Java than this code?
      • https://stackoverflow.com/questions/1137488/ways-of-producing-xml-in-java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to turn a program I have into a service so I can
I have an projector file/Flash application that I need to turn into an interactive
I have a set of points that i want to turn into a closed
I have an air application that I'd like to turn into a multi-player game.
I have an xml template document that I need to load into an XmlDocument.
In Org-mode I want to a region into a list. Say that I have
I have a Java program that runs on Linux and telnets into a remote
I have to turn in a hard copy of some code with an assignment.
I have some experience making multiplayer turn-based games using sockets, but I've never attempted
I have object A which in turn has a property of type Object B

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.