Uday Konduru
Uday Konduru

Reputation: 213

Need to convert from XML to HTML in java

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?

Upvotes: 1

Views: 10647

Answers (3)

Amit Biton
Amit Biton

Reputation: 9

You can use StringEscapeUtils and use the method escapeHtml.

String yourXmlAsHtmlString = StringEscapeUtils.escapeHtml(yourXmlAsString);

Upvotes: -1

acdcjunior
acdcjunior

Reputation: 135742

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.

Upvotes: 1

NFE
NFE

Reputation: 1187

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!');
  })
;

Upvotes: 1

Related Questions