Support Questions
Find answers, ask questions, and share your expertise

Dynamically Parsing XML data in Hive Column and storing into Map

New Contributor

Hey all,

I have a column in hive that xml data, however the xml format is not static, it changes based on a category column and there are alot. I wanted to know if anybody knows if there is a way to programmaitcally take out the tag and the value in the xml column and store it in map column. Best thing I found is xpath in hive however I need to declare what I am taking out. I can do it with a script via java/scala/python however I would just be connecting to Hive via Jdbc and pulling the data out and doing the work client side. And Unfortunately we do not have spark which I think would be able to do it in.

Any ideas?


@Neil Patel

Most flexible way is to use XML Serde ( where you can read XML using map and also nested maps/arrays.

The problem is the XML Serde does not work when you already have a table / column with xml, you would need to use their internal functions and write your own UDFs.

The workaround would be to create one table only with your XML column, save as text format, use XML Serde to read textfile with XML content translating them into maps/arrays and finally join result back with your original table.

Also more examples here:

New Contributor

Im looking at the examples and they don't seem to address my issue of dynamically getting the tag and value, in the examples they declaring which tags to parse. I can do that already with xpath.

Look at this example:

    <record customer_id="0000-JTALA">
CREATE TABLE xml_bank(customer_id STRING, income BIGINT, demographics map<string,string>, financial map<string,string>)
"xmlinput.start"="<record customer",

select customer, income demographics, financial from xml_bank limit 1; 
{"gender":"F", "agecat":"1", "edcat":"1", "jobcat":"2", "empcat":"2", "retire":"0", "jobsat":"1", "marital":"1", "spousedcat":"1", "residecat":"4", "homeown":"0", "hometype":"2", "addresscat":"2"}
{"income": "18", "creddebt": "1.003392", "othdebt": "2.740608", "default": "0"}

New Contributor

Ahh okay thank you makes much more sense now.

Unfortunately we decided to just a create custom python udf to parse the xml column, this will be easier for us since we wouldn't need a seperate process just to extract the xml into another file.

New Contributor

New Contributor