【问题标题】:XML to Database in PythonPython中的XML到数据库
【发布时间】:2020-03-09 05:10:02
【问题描述】:

我有数千个 XML 文件

它有很多标签和很多属性。

我想把数据放到 SQL Server 表中。

表格的结构会是这样的

DocumentPath varchar(1000)
Tag          varchar(1000)
ID           varchar(1000)
Attribute    varchar(1000)
Value        varchar(1000)

XML 文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmlns:util="http:///org/" xmi:version="2.0">
    <cas:NULL xmi:id="0"/>
    <tcas:DocumentAnnotation xmi:id="8" sofa="1" begin="0" end="5769" language="x-unspecified"/>
    <structured:DocumentID xmi:id="13" documentID="5"/>
    <structured:DocumentIdPrefix xmi:id="15" documentIdPrefix=""/>
    <structured:DocumentPath xmi:id="35" documentPath="J:\T9.xmi"/>
    <textspan:Segment xmi:id="37" sofa="1" begin="0" end="5769" id="SIMPLE_SEGMENT" preferredText="SIMPLE_SEGMENT"/>
    <textspan:Sentence xmi:id="44" sofa="1" begin="0" end="15" sentenceNumber="0"/>
    <textspan:Sentence xmi:id="50" sofa="1" begin="17" end="32" sentenceNumber="1"/>
    <textspan:Sentence xmi:id="56" sofa="1" begin="37" end="52" sentenceNumber="2"/>
    <syntax:SymbolToken xmi:id="2242" sofa="1" begin="18" end="19" tokenNumber="4"/>
    <syntax:SymbolToken xmi:id="2250" sofa="1" begin="19" end="20" tokenNumber="5"/>
    <syntax:SymbolToken xmi:id="2301" sofa="1" begin="29" end="30" tokenNumber="11"/>
    <syntax:SymbolToken xmi:id="2309" sofa="1" begin="30" end="31" tokenNumber="12"/>
    <syntax:NumToken xmi:id="2258" sofa="1" begin="20" end="24" tokenNumber="6" numType="1"/>
    <syntax:NumToken xmi:id="2275" sofa="1" begin="25" end="26" tokenNumber="8" numType="1"/>
    <syntax:NumToken xmi:id="2292" sofa="1" begin="27" end="29" tokenNumber="10" numType="1"/>
    <syntax:NumToken xmi:id="2381" sofa="1" begin="57" end="61" tokenNumber="20" numType="1"/>
    <textsem:MedicationMention xmi:id="18523" sofa="1" begin="534" end="540" id="0" ontologyConceptArr="18510" typeID="1" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:MedicationMention xmi:id="16831" sofa="1" begin="543" end="547" id="0" ontologyConceptArr="16818" typeID="1" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:MedicationMention xmi:id="16788" sofa="1" begin="558" end="569" id="0" ontologyConceptArr="16753 16773 16763" typeID="1" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:ProcedureMention xmi:id="22518" sofa="1" begin="328" end="343" id="0" ontologyConceptArr="22505" typeID="5" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:ProcedureMention xmi:id="22682" sofa="1" begin="478" end="490" id="0" ontologyConceptArr="22669" typeID="5" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:ProcedureMention xmi:id="22228" sofa="1" begin="495" end="510" id="0" ontologyConceptArr="22215" typeID="5" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:ProcedureMention xmi:id="21938" sofa="1" begin="794" end="809" id="0" ontologyConceptArr="21925" typeID="5" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:ProcedureMention xmi:id="22868" sofa="1" begin="1057" end="1072" id="0" ontologyConceptArr="22855" typeID="5" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:AnatomicalSiteMention xmi:id="24333" sofa="1" begin="786" end="793" id="0" ontologyConceptArr="24320" typeID="6" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:AnatomicalSiteMention xmi:id="25103" sofa="1" begin="842" end="857" id="0" ontologyConceptArr="25090" typeID="6" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:AnatomicalSiteMention xmi:id="23654" sofa="1" begin="842" end="850" id="0" ontologyConceptArr="23641" typeID="6" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:EventMention xmi:id="25137" sofa="1" begin="117" end="124" id="0" ontologyConceptArr="25124" typeID="1003" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:EventMention xmi:id="25201" sofa="1" begin="3448" end="3458" id="0" ontologyConceptArr="25188" typeID="1003" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <textsem:EventMention xmi:id="25169" sofa="1" begin="3796" end="3803" id="0" ontologyConceptArr="25156" typeID="1003" discoveryTechnique="1" confidence="0.0" polarity="0" uncertainty="0" conditional="false" generic="false" historyOf="0"/>
    <cas:Sofa xmi:id="1" sofaNum="1" sofaID="_InitialView" mimeType="text"/>
    <structured:SourceData xmi:id="23" noteTypeCode="ClinicalNote" sourceRevisionNbr="0" sourceRevisionDate="2020-03-02 10:48:30"/>
    <refsem:UmlsConcept xmi:id="18510" codingScheme="SNOMEDCT_US" code="64197008" score="0.0" disambiguated="false" cui="C0037366" tui="T131" preferredText="Smoke"/>
    <refsem:UmlsConcept xmi:id="16818" codingScheme="RXNORM" code="746839" score="0.0" disambiguated="false" cui="C1999262" tui="T122" preferredText="Pack"/>
    <refsem:UmlsConcept xmi:id="16753" codingScheme="SNOMEDCT_US" code="410942007" score="0.0" disambiguated="false" cui="C0013227" tui="T121" preferredText="Pharmaceutical Preparations"/>
    <cas:View sofa="1" members="8 13 15 17 35"/>
</xmi:XMI>

structured:DocumentPath 标签和documentPath attibute 在表格中的值将是DocumentPath

Tag 在 XML 标记的每一行中,例如textspan:Sentence

ID 是 xmi:id 值

Attribute 是每个属性名 而Value是属性值

因此这一行

<textspan:Sentence xmi:id="44" sofa="1" begin="0" end="15" sentenceNumber="0"/>

会像这样在表格中

DocumentPath  Tag                 ID    Attribute         Value        
J:\T9.xmi     textspan:Sentence   44    sofa              1
J:\T9.xmi     textspan:Sentence   44    begin             0
J:\T9.xmi     textspan:Sentence   44    end               15
J:\T9.xmi     textspan:Sentence   44    sentenceNumber    0

我发现这个网站展示了如何读取一个简单的 XML 文件

但我的情况与每行中的属性数量不同是动态的

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 方法是将数据转换为列表,然后导出到Sql Server。您需要读取每个标签和每个属性(因此即使它是动态的,它也会缓存它)。一些可以在这里提供帮助的包:lxml.etree - 用于从 XML 中读取和提取数据。 pyodbc - 将数据移动到 Sql Server。
  • 我也做过类似的案例,我用一个python包untangle将XML解析为python对象,然后你就很容易操作数据了,然后用sqlalchemy插入数据DB,你绝对可以试试untangle。仅供参考:pip install untangle 希望对您有所帮助!
  • @panda912 可以分享一下代码吗?

标签: python sql-server xml database


【解决方案1】:

尝试pip intall untangle 安装untangle。 一个示例使用您的输入:

from untangle import parse
your_xml = """.. omit.."""
your_obj = parse(your_xml)
your_obj.xmi_XMI.textspan_Segment._attributes

{'xmi:id': '37',
 'sofa': '1',
 'begin': '0',
 'end': '5769',
 'id': 'SIMPLE_SEGMENT',
 'preferredText': 'SIMPLE_SEGMENT'}

如您所见,所有数据都可以像对象一样访问。

【讨论】:

  • 谢谢,但是当我运行 your_obj=parse(your_xml) 时出现此错误 .. 错误是“Traceback(最近一次调用最后一次):文件“”,第 1 行,在 your_obj = parse(your_xml) 文件 "C:\Users\ASMGX\Anaconda3\lib\site-packages\untangle.py",第 176 行,在 parse if is_string(filename) and (os.path .exists(filename) or is_url(filename)): File "C:\Users\ASMGX\Anaconda3\lib\genericpath.py", line 19, in exists os.stat(path) ValueError: stat: path too long for Windows "
  • 您的路径似乎太长了?试着把它放到另一个文件夹,你可以用your_xml_str = """xml_content"""粘贴你的XML像str wrapper,或者你也可以像文件一样打开它来读取。
  • 没有在 windows 上尝试过,所以你现在得到了你的 xml 对象?
猜你喜欢
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多