XhCode Online Converter Tools

HTML Table To XML Converter

Enter html here:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Results:
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
HTML Table To XML

Converting an HTML table to XML format involves taking the data from an HTML table and generating XML tags for each element in the table. This is useful when you want to convert your table into a machine-readable format like XML, which can be processed by other applications or databases.

Example HTML Table:
html

<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>
Converted XML Output:
xml

<table>
<row>
<Name>John</Name>
<Age>28</Age>
<Country>USA</Country>
</row>
<row>
<Name>Jane</Name>
<Age>22</Age>
<Country>Canada</Country>
</row>
<row>
<Name>Tom</Name>
<Age>30</Age>
<Country>UK</Country>
</row>
</table>
How to Convert HTML Table to XML
1. Using JavaScript (For Web Use)
You can use JavaScript to extract data from an HTML table and generate XML tags dynamically. Here's an example of how to achieve this in a browser:

JavaScript Code:
html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Table to XML</title>
</head>
<body>
<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>

<button onclick="generateXML()">Generate XML</button>
<pre id="xml-output"></pre>

<script>
function generateXML() {
var table = document.getElementById("data-table");
var rows = table.rows;
var xml = '<table>\n';

// Loop through each row (skip the header row)
for (var i = 1; i < rows.length; i++) {
var row = rows[i];
xml += ' <row>\n';

for (var j = 0; j < row.cells.length; j++) {
var header = table.rows[0].cells[j].textContent.trim();
var value = row.cells[j].textContent.trim();
xml += ` <${header}>${value}</${header}>\n`;
}

xml += ' </row>\n';
}

xml += '</table>';
document.getElementById("xml-output").textContent = xml;
}
</script>
</body>
</html>
Explanation:
The generateXML() function loops through the rows of the HTML table (skipping the header row) and generates an XML structure where each cell's content is wrapped in an XML tag.
Each row from the table is represented as <row>, and the header cells (<th>) are used as XML tag names.
The XML output is displayed in the <pre> tag to preserve formatting.
Steps:
Copy and paste the code into an HTML file.
Open the file in a web browser.
Click the "Generate XML" button to generate the XML representation of the HTML table.
2. Using Python (For Automated or Bulk Conversion)
For bulk conversions or automation, you can use Python with BeautifulSoup to extract the HTML table data and convert it into XML format.

Python Script:
python

from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET

def html_table_to_xml(html_file, output_file):
with open(html_file, 'r') as file:
soup = BeautifulSoup(file, 'html.parser')
table = soup.find('table')

# Create the root of the XML structure
table_xml = ET.Element("table")

# Get the headers (column names)
headers = [header.text.strip() for header in table.find_all('th')]

# Loop through each row and create XML structure for each cell
for row in table.find_all('tr')[1:]: # Skip header row
cells = row.find_all('td')
if len(cells) > 0:
row_xml = ET.SubElement(table_xml, "row")
for i, cell in enumerate(cells):
# Use header name as XML tag and cell text as value
ET.SubElement(row_xml, headers[i]).text = cell.text.strip()

# Create an XML tree and write it to a file
tree = ET.ElementTree(table_xml)
tree.write(output_file)

# Example usage
html_table_to_xml('your_file.html', 'output.xml')
Explanation:
BeautifulSoup is used to parse the HTML table and extract its contents.
The xml.etree.ElementTree module is used to build the XML structure by creating XML tags for each row and cell.
The header row (<th>) is used to define the XML tags for each column, and each cell's content is added as text for the corresponding tag.
The resulting XML is saved to a file.
Steps:
Install the required libraries: pip install beautifulsoup4 lxml.
Run the script with the path to your HTML file and the desired output XML file.
The script will generate the XML file and save it.
3. Using Online Tools:
You can also use online tools to convert HTML tables into XML format:

TableConvert:

Paste your HTML code or upload an HTML file.
The tool will automatically generate the XML representation of your table.
You can download the XML file or copy the XML code to your clipboard.
ConvertCSV:

Upload your HTML file or paste the table data.
The tool will convert the HTML table into an XML structure, which you can then download or copy.
4. Manually Writing XML:
For small tables, you can manually write the XML output by using the data in your HTML table. For example:

html

<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
</table>
The manually written XML would be:

xml

<table>
<row>
<Name>John</Name>
<Age>28</Age>
<Country>USA</Country>
</row>
<row>
<Name>Jane</Name>
<Age>22</Age>
<Country>Canada</Country>
</row>
</table>
Summary of Methods:
JavaScript: Use JavaScript to convert the HTML table to XML directly in the browser. Great for on-the-fly conversions.
Python: Use Python and BeautifulSoup for more control and automation, ideal for handling multiple tables or large datasets.
Online Tools: Websites like TableConvert and ConvertCSV allow you to quickly convert HTML tables to XML without needing to write any code.
Manual Writing: For small tables, you can manually write the XML structure based on the table data.