-1

Good Day! I can't figure out how to make a correct XML-file.

Here is what i have tried as code:

outFile.println("<ProjectsData>");
    for (Project project : projects) {
                outFile.println("<Project>" + project.getName() + "</Project>");
                outFile.println("<ProjectID>" + project.getProjectId() + "</ProjectID>");
                outFile.println("<License>" + project.getLicenseId() + "</License>");}
outFile.println("</ProjectsData>");

Is it necessary that the ID and the license are under the product should be aligned like that:

<ProjectsData>
   <Project="bsssk101">
      <ProjectID>101Y</ProjectID>
      <License>XML Developer's Guide</License>
   </Project>
</ProjectsData>

if yes... How to do it?

  • Not necessary... But formatted content is better in understanding... if you want to format the data like that then add a particular no of spaces before the lines... also your code data doesn't matches with the xml data you have shown.. please correct it... – CoderNeji Jul 27 '15 at 11:22
  • @CoderNeji I don't quite understand you. What do you mean? Can you give more detail about how to do? Sorry for the stupid questions – IvanushkaDurachok Jul 27 '15 at 11:42
  • possible duplicate of [How to pretty print XML from Java?](http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java) – Robert Moskal Jul 27 '15 at 11:50
  • @RobertMoskal all the solutions in the linked article reindent an existing xml document or string. The OP wants to build up a new XML document himself and have pretty indents in that process. – wero Jul 27 '15 at 12:04
  • Uee JAXB - short example http://examples.javacodegeeks.com/core-java/xml/bind/jaxb-marshal-example/ – user1516873 Jul 27 '15 at 13:06

1 Answers1

1

You can use this function to format any XML String. Try this. Create a raw xml using your logic and pass it to this function it will take care of indent and all.

    public static String formatXMLString(String unformattedXml) throws Exception {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(unformattedXml)));
        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setOmitXMLDeclaration(true);
        format.setIndent(2);
        Writer out = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);
        return out.toString();
    } catch (IOException e) {
        throw e;
    }
}

UPDATE

We can use JAXB to unmarshl the Object and get a formatted XML.

public static void main(String[] args) throws Exception {
    StringWriter writer = new StringWriter();
    Project project = new Project("1", "POP");
    JAXBContext contect = JAXBContext.newInstance(project.getClass());
    Marshaller marshaller = contect.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(project, writer);
    System.out.println( writer.toString());

}
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48