Dom4j converts sting type XML into HTML table code

Source: Internet
Author: User

Import java. Lang. String;
Import java. RMI. RemoteException;
Import java. util. iterator;
Import java. util. List;

Import org. Apache. axis2.axisfault;
Import org. dom4j .*;

Public class user
{

// Convert an XML string into an HTML language in the form of a table
Public static string outputintable (string XML, string trclass, string tdclass)
{
// Html of the output result
String table = "";
// Output the HTML of the table Header
String tablehead = "";
Try
{
// Read string-type XML and convert it to document
Document Doc = incluenthelper. parsetext (XML );
// Get the XML Root Node
// Element root = Doc. getrootelement ();

// Obtain the list of child nodes of the root element
// System. Out. println (DOM. getnode (Doc, "/dataset/table/ID"). gettext ());

// Obtain the location of all tables
List L = Doc. selectnodes ("/dataset/table ");
Iterator headiter = L. iterator ();
Iterator contentiter = L. iterator ();

// Retrieve the header part: retrieve all column headers from the first table
Element headtmp = (element) headiter. Next ();


List headtd = headtmp. Elements ();
Iterator headtditer = headtd. iterator ();

Tablehead = "<TR" + "class =" + trclass + "> ";
// Store the names of all elements in the first table to tablehead.
While (headtditer. hasnext ())
{
Element headtmpp = (element) headtditer. Next ();
Tablehead + = "<TD" + "class =" + tdclass + ">" + headtmpp. getname () + "</TD> ";
// System. Out. println (TMP. asxml () + "/N ");
}
Tablehead + = "</tr> ";

// Use the data in XML as the data in the table
// Obtain the element values cyclically in each table
While (contentiter. hasnext ())
{
Element TMP = (element) contentiter. Next ();
List TD = TMP. Elements ();
Iterator tditer = TD. iterator ();
Table + = "<TR" + "class =" + trclass + "> ";
// Obtain the values of each element in the table
While (tditer. hasnext ())
{
Element tmpp = (element) tditer. Next ();
Table + = "<TD" + "class =" + tdclass + ">" + tmpp. gettext () + "</TD> ";
}
// System. Out. println (TMP. asxml () + "/N ");

Table + = "</tr>/N ";
}
Table = tablehead + "/N" + table;
}
Catch (incluentexception E)
{
E. printstacktrace ();
}
Return table;
}

Public static void main (string [] ARGs)
{
String xml = "<dataset> <Table> <ID> 1 </ID> <serviceid> 0 </serviceid> <servicename> basic </servicename> <memo> basic </ memo> </table> <Table> <ID> 2 </ID> <serviceid> 1 </serviceid> <servicename> SMS </servicename> <memo> SMS notification </ memo> </table> <Table> <ID> 3 </ID> <serviceid> 2 </serviceid> <servicename> VoIP </servicename> <memo> VoIP </memo> </table> <Table> <ID> 4 </ID> <serviceid> 3 </serviceid> <servicename> diskspace </servicename> <memo> large disk space </ memo> </table> </dataset> ";
System. Out. println (user. outputintable (XML, "trclass", "tdclass "));
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.