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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:42:20+00:00 2026-06-09T09:42:20+00:00

I have following method: private <U> void fun(U u) throws JAXBException { JAXBContext context

  • 0

I have following method:

private <U> void fun(U u) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(u.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(u, System.out);
    }

marshal() method takes different types of arguments. See here
It takes:

  1. ContentHandler
  2. OutputStream
  3. XmlEventWriter
  4. XMLStreamWriter etc.

How to modify the above method such that instead of System.out , I can pass the destination of marshalling in function argument.

For e.g. I want to call method like:

objToXml(obj1,System.out);
outToXml(obj1,file_pointer);

Likewise.

I tried following with fun(obj1,PrintStream.class,System.out) but it was unsuccessfull:

private <T, U, V> void fun(T t, Class<U> u, V v) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(t.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(t, (U) v);
    }
  • 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-09T09:42:22+00:00Added an answer on June 9, 2026 at 9:42 am

    You don’t need to add additional generic parameters to your method. Simply pass a javax.xml.transform.Result to the marshaller:

    private <U> void fun(U u, Result result) {
      JAXBContext context = JAXBContext.newInstance(u.getClass());
      Marshaller marshaller = context.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(u, result);
    }
    

    You can use the StreamResult for writing to System.out or to a file:

    fun(foo, new StreamResult(System.out));
    fun(foo, new StreamResult(file_pointer.openOutputStream()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method in my WPF project (.net 4): private void MyMethod(){
I have the following method: private <E extends Number> void AddOne(ArrayList<E> al, Double num)
I have the following method that changes the language of the winform. private void
I have following simple method in my main activity. private void showMap(String name )
I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
I have a following method private void SetProcessDocumentStatus(string status) { var setStatusWith = new
I'm working with JPA 2 and have the following method: private static void wipeTable(EntityManager
I have the following method within my TcpServer private void Receive(ZClient client) { NetworkStream
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
I have the following C# method: private string[] test() { } I would like

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.