can you please help me with parsing xml files into tables? I have in my SQL database thousands of xml files like this:
These xml files got this structure:
`<eolresult>
<eolpresetting>
<datetime>08.12.2020 5:24:48</datetime>
<order id="2049457704XSK316_FRX101xxxxxxxxxxxxxx000SK35X3PH9P40006C24A4NORL0L7P7XYA0000006T24D0XXK0006A00000000" />
</eolpresetting>
<testresult>NIO</testresult>
<results>
<!--PRN="2049457704XSK316_FRX101xxxxxxxxxxxxxx000SK35X3PH9P40006C24A4NORL0L7P7XYA0000006T24D0XXK0006A00000000"-->
<test name="Time">
<value name="Time of start ">08.12.2020 5:23:03</value>
<value name="Time of save ">08.12.2020 5:24:48</value>
<value name="Time ">105 s</value>
</test>
<test name="Manual test">
<value name="Manual test was used: ">False</value>
</test>
<test name="Airbag SAB">
<value name="Measured value - Generator">2,111209</value>
<value name="Low limit - Generator">1,74</value>
<value name="High limit - Generator">2,26</value>
<value name="Measured value - Break contact 1">5,069321</value>
<value name="Minumum value - Break contact 1">1,73</value>
<value name="Measured value - Break contact 2">5,092026</value>
<value name="Minumum value - Break contact 2">1,73</value>
<value name="Test OK">True</value>
</test>
<test name="Heating">
<value name="Measured value">1,294227</value>
<value name="Low limit">1,22</value>
<value name="High limit">1,49</value>
<value name="Test OK">True</value>
</test>
<test name="Thermistor">
<value name="Measured value">8671,408</value>
<value name="Low limit">4000</value>
<value name="High limit">14000</value>
<value name="Test OK">True</value>
</test>
<test name="Buckle">
<value name="Measured value 1 - unbuckled">9,730747E-02</value>
<value name="Measured value 2 - buckled">25022,52</value>
<value name="Measured value 3 - unbuckled">8,596408E-02</value>
<value name="Bucked limit">3,92</value>
<value name="Unbuckled limit">999</value>
<value name="Test OK">True</value>
</test>
<test name="SBR">
<value name="Measured value - Low force">99999,9</value>
<value name="Low limit - Low force">332</value>
<value name="High limit - Low force">468</value>
<value name="Measured value - Low force - Force [kg]">5,242241</value>
<value name="Low limit - Low force - Force [kg]">5,1</value>
<value name="High limit - Low force - Force [kg]">6,4</value>
<value name="Regulator Active - Low force">False</value>
<value name="Measured value - High force">99999,9</value>
<value name="Low limit - High force">83</value>
<value name="High limit - High force">117</value>
<value name="Measured value - High force - Force [kg]">33,80444</value>
<value name="Low limit - High force - Force [kg]">33,6</value>
<value name="High limit - High force - Force [kg]">34,3</value>
<value name="Regulator Active - High force">True</value>
<value name="Strain Gauge - Tare [kg]">-10,77971</value>
<value name="Test OK">False</value>
</test>
<test name="Light">
<value name="Measured value">21,33521</value>
<value name="Low limit">10</value>
<value name="High limit">40</value>
<value name="Test OK">True</value>
</test>
<test name="Headrests">
<value name="Force - Measured">16,04999</value>
<value name="Force - Low Limit">15,81</value>
<value name="Force - High Limit">17,64</value>
<value name="Regulator Active">True</value>
<value name="Strain Gauge - Tare [kg]">-8,725739E-02</value>
<value name="Test OK">True</value>
</test>
<test name="Delivery">
<value name="Measured value - Backrest">0</value>
<value name="Low limit - Backrest">0</value>
<value name="High limit - Backrest">0</value>
<value name="Measured value - Forwards">0</value>
<value name="Low limit - Forwards">0</value>
<value name="High limit - Forwards">0</value>
<value name="Measured value - Height">0</value>
<value name="Low limit - Height">0</value>
<value name="High limit - Height">0</value>
<value name="Measured value - Thigh">0</value>
<value name="Low limit - Thigh">0</value>
<value name="High limit - Thigh">0</value>
<value name="Measured value - Headrest">384,2014</value>
<value name="Low limit - Headrest">320</value>
<value name="High limit - Headrest">450</value>
<value name="Delivery OK">False</value>
</test>
</results>
</eolresult>`
And I need to somehowe parse all of these xml files to tables for. example by the "Test name" because I would like to create a report from these data via Power BI. Or if there is some another way how to process these files, I will be glad for every idea.
What I imagine is that the tables would look something like this. Each table contain the ID, the date from the eolpresenting and the values from the tests. Example:
2条答案
按热度按时间jdgnovmf1#
You don't need a cursor here, you can just use
.nodes
directly in anINSERT
.You can even cross-join one set of
.nodes
with another, and you can also pass a single node to.nodes
to shred a second level.db<>fiddle
If you are using an actual table with the data, you can change the
FROM
to something like this:This is all the code you need, no cursors necessary. Note the use of
text()
which is more performant than an implicit conversion from.
You may want to further transform it into separate tables, for that you can use XQuery predicates, such as this
db<>fiddle
wnvonmuf2#
To parse
XML
files and store the data in tables inMS SQL,
you can use the SQL Server's built-in XML functionalityWrite a script or
stored procedure
to parse theXML
files and insert the data into the tables.The nested cursors are structured to process the test and value nodes within the XML data.
Here is the template you can adapt
In the above replace
YourTableName
in thecursor
declaration with the actual table name and column name where your XML files are stored.This code should parse the
XML files
, extract the required data, and store it in theEolResultData
table, which has columns forID, DateTime, TestName, ValueName, and Value
.Update:
Assuming multiple
XML
records are present under the<eolresults>
element, it can handle them correctly without usingcursors
as someone suggested.The modified template here uses the
nodes() method
to extract individual<eolpresetting> nodes
and then performs anINSERT INTO SELECT
operation to insert the data into theEolResultData
table. It has the extraorderId col