1

Please give me suggestion as i need to convert from XML to HTML in java without using XSLT. As i was searching in the web but everywhere it was showing can convert from xml to html with use of only xslt/xsl?

Please guyz give me some suggestions?

5
  • What do you mean XML to HTML, exactly??
    – acdcjunior
    Jun 16, 2013 at 6:26
  • i had one xml file which i need to convert and show into html page or in html format.As i dont have any xsl file for the same. Jun 16, 2013 at 6:30
  • NFE answer below is the best solution for this simple usecase. Keep XML in original format and use a HTML page with javascript to parse and present it in the browser. Jun 16, 2013 at 7:01
  • 1
    "..without using XSLT." Why 'without'? It is an excellent tool for the job. Jun 16, 2013 at 9:25
  • @AndrewThompson---as i dont have any xsl and dont know how to write xsl for the same.And i dont have enough time to learn and write our own xsl.So i am trying and looking for different apporach Jun 17, 2013 at 5:06

3 Answers 3

1

You can parse xml data using jQuery.parseXML and use data of it.

$.get('/url_of_the_xml_resource')
  .done(function(data){
    // parse the xml
    data = $.parseXML(data);
    //
    // do anything you want with the parsed data
  })
  .fail(function(){
    alert('something went wrong!');
  })
;
0
1

This will save root.xml's content as root.xml.html.

public static void main(String[] args) throws Exception {
    String xmlFile = "root.xml";
    Scanner scanner = new Scanner(new File(xmlFile)).useDelimiter("\\Z");
    String xmlContent = scanner.next();
    xmlContent = xmlContent.trim().replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("\n", "<br />").replaceAll(" ", "&nbsp;");
    PrintWriter out = new PrintWriter(xmlFile+".html");
    out.println("<html><body>" + xmlContent + "</body></html>");
    scanner.close();
    out.close();
}

Note: This will retain the XML's original indentation and line breaking.

5
  • thanks for your input...let me try this also..but i want to know what do you mean by retain the XML's original indentation? Jun 16, 2013 at 7:02
  • In the xmlContent line, remove the .replaceAll("\n", "<br />").replaceAll(" ", "&nbsp;") and the indentation will not be retained (then you'll see what I mean).
    – acdcjunior
    Jun 16, 2013 at 7:08
  • actually i want the xml content in some kind of sorting order in html page.But as per your suggestion i tried it was just outputing in html format Jun 17, 2013 at 5:05
  • You should update your questions with those additional requirements, then - we can't help you with that if we don't know you need it.
    – acdcjunior
    Jun 17, 2013 at 12:53
  • yes in the starting it was simple one only..but later scenario got changed..sorry for the same Jun 18, 2013 at 6:58
-1

You can use StringEscapeUtils and use the method escapeHtml.

String yourXmlAsHtmlString = StringEscapeUtils.escapeHtml(yourXmlAsString);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.