Results 1 to 5 of 5

Thread: Help in XML

  1. #1
    ims6674 is offline Member
    Join Date
    Apr 2007
    Location
    India
    Posts
    2
    Rep Power
    0

    Unhappy Help in XML

    Hi,

    I am new to XML and as my first assignment I got a task to generate a XML file. I am able to generate it with a lengthy procedure, however I want it to generate in generic way.

    Could you please help me?

    Thanks,
    Imran.

  2. #2
    levent Guest

    Default

    Hi Imran,

    Can you give more details about your problem/solution. What do you mean with lengthy procedure?

    If you can describe your problem more, we can help. You can paste your source code and XML sample too if they will help us understand your problem.

  3. #3
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Jaxb is a great package for generating xml files. I put some sample code below. But, it depends on exactly how you are obtaining the data to put into your xml file. If you do not have the data anyplace you are going to have to enter it, maybe in a database , then use jaxb to format it into xml files. Anyway, maybe you can explain in detail what you are doing so we can offer specific help.
    Here is some generic jaxb code:
    Java Code:
    JAXBContext jaxbContext = JAXBContext.newInstance(MyBean.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output",new Boolean(true)); 
    //gather data method would be here, fill in your beans		
    File xml = new File(baseDir, "myXMLFIle.xml");
    marshaller.marshal( listOfBeans, new FileOutputStream(xml));

  4. #4
    ims6674 is offline Member
    Join Date
    Apr 2007
    Location
    India
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    Thanks for your reply.

    I have to generate a xml file which includes 45-50 tag elements depending upon the user's input i.e. dynamically. Few of them are having child nodes.

    I am able to generate this file by writting each and every tag element, adding its attribute if present then adding the text node.

    I want to ask, Is there any way so that I can generalize my code by
    calling any method so that to reduce the code length?

    I was also able to write one method, to which I am passing the tag element and its text node value, however it fails if that tag element is having some child nodes.

    Could you please find some solution to this?

    Thanks,
    Imran.

  5. #5
    levent Guest

    Default

    Hi Imran,

    I did not use XStream, but as far as i see you can create an object, set its fields based on user input and serialize it to XML with XStream. I guess it will have less numbers of lines and easier to maintain compared to manual serialization.

    As far as i see, it is also possible to do this with JAXB. Check following tech tip:

    Object Serialization with the JAXB Libraries and Revisiting Timers with Enterprise Beans Tech Tips
    Last edited by levent; 04-24-2007 at 12:29 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •