xml输出格式不合理的原因

时间:2019-07-17 16:31:31

标签: xml

这真让我发疯。我认为没有任何理由。我对为什么要这样做的想法已经不多了,我希望有人可以给我一个理由。问题是这样的:我有一个xml文件,正在修改以取消标识患者信息。 xml中的一行让我头疼,我不知道为什么。文件中的原始行是:

<id root="2.16.840.1.999999.3.109.3.2722.4.1.1.999999.2.9" extension="1999552"/>

当我修改该行并将其写出

<id extension="123456" 
 root="2.16.840.1.999999.3.109.3.2722.4.1.1.999999.2.9" 

我只修改扩展名值。对于电信价值,我也有类似的说法,并且正确地将其写出:发件人:

<telecom use="WP" value="tel:1(917)999-0899"/>

收件人:

<telecom use="WP" value="tel:1(800)999-9999"/>

我拿出了修改扩展名值的代码,只是修改了根值而添加了代码,但仍将其写出来交换了出来。我尝试了两种不同的方法,一种使用与电信相同的代码,但是没有用,然后尝试了另一种方法,也没有用。 在这一点上,我很沮丧。我可能正在盯着原因,但我看不到它。如果有人可以看一下,让我知道为什么仅在这条线上发生,我将非常感激。

使用已经可以使用的相同代码,更改标签和属性值以匹配我需要的代码,还尝试了一种具有完全相同结果的新代码方法。在论坛中搜索答案无济于事。 这是xml文件的片段:

<recordTarget>
    <patientRole>
      <id root="2.16.840.1.999999.3.109.3.2722.4.1.1.999999.2.9" extension="1999552"/>
      <addr>
        <streetAddressLine>999 E 11th St</streetAddressLine>
        <city>Galena</city>
        <state>CO</state>
        <postalCode>81099</postalCode>
        <country>US</country>
      </addr>
      <telecom use="WP" value="tel:1(917)999-0899"/>
      <telecom use="MC" value="tel:1(917)999-0899"/>
      <patient>

#this approach does not work for the id root/extension
for mrn in doc.getElementsByTagName("id"):
 mrnID = mrn.getAttribute("extension")
 mrn.setAttribute("extension", "123456")

#Also Tried this approach and it didn't work as well
#Just tried writing out ONLY the id root value to see if that made
#a difference. It didn't values where still swapped even though I never
#operated on the extension attibute
if doc.getElementsByTagName("patientRole"):   
  for mrn in doc.getElementsByTagName("id"):
   rootID = mrn.getAttribute("root")
   oidID = rootID 
   mrn.setAttribute("root", oidID)

#telecom section this works for the telecom values
 for telecom in doc.getElementsByTagName("telecom"):
  phone = telecom.getAttribute("value")
  if phone[0:4] in ["mail"]:
   telecom.setAttribute("value", "mailto:anon@anon.com")
  if phone[0:3] in ["tel"]: 
   telecom.setAttribute("value", whichPhone)

#AT the end, this is how I write out the file
#outFile = "mod-" + inputName + ".xml"
 os.chdir(anonymizedDir)
 f = open(outFile, "w")
 f.write(xml_pretty_str)
 f.close()

0 个答案:

没有答案