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?? Commented 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. Commented 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. Commented Jun 16, 2013 at 7:01
  • 1
    "..without using XSLT." Why 'without'? It is an excellent tool for the job. Commented 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 Commented 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!');
  })
;
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

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?
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).
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
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.
yes in the starting it was simple one only..but later scenario got changed..sorry for the same
-1

You can use StringEscapeUtils and use the method escapeHtml.

String yourXmlAsHtmlString = StringEscapeUtils.escapeHtml(yourXmlAsString);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.