当前位置 : 主页 > 编程语言 > delphi >

delphi 7读取和处理xml文件的方式和组件 – 更新

来源:互联网 收集:自由互联 发布时间:2021-06-23
我有一个客户端提供带有混合逗号分隔数据和xml的文件.分离的逗号不是问题,但xml对我来说都是新手. 我试图找到一个组件来做我需要的东西(omnixml -abandoned – 使用内置的xml组件中的de
我有一个客户端提供带有混合逗号分隔数据和xml的文件.分离的逗号不是问题,但xml对我来说都是新手.

我试图找到一个组件来做我需要的东西(omnixml -abandoned – 使用内置的xml组件中的delphi)似乎可能…

我有以下数据:

<Passengers>
  <Passenger>
<No>1</No>
<Title>mrs</Title>
<ForeName>Anne</ForeName>
<SurName>XXXXXXXX</SurName>
<Age>33</Age>
<UWStatus>accept</UWStatus>
<Screening>
  <ScreeningData>
    <ScreeningPath SL="2.2" DATA="1">
      <MedicalRisk>4.01</MedicalRisk>
      <rootConditionId>1292</rootConditionId>
      <isAMT>false</isAMT>
      <regionId>4</regionId>
      <isWinterSport>false</isWinterSport>
      <isRetScheme>false</isRetScheme>
      <isPair>false</isPair>
      <LinkedCondition>3</LinkedCondition>
      <LinkedConditions>
        <LinkedCondition Name="High blood pressure" ICD="401.9" Type="D"/>
        <LinkedCondition Name="Renal failure" ICD="586" Type="D"/>
        <LinkedCondition Name="Abdominal aortic aneurysm" ICD="441.4" Type="I"/>
        <LinkedCondition Name="Peripheral vascular disease" ICD="443.9" Type="I"/>
        <LinkedCondition Name="Angina" ICD="414.9" Type="IS"/>
        <LinkedCondition Name="Enlarged heart" ICD="425" Type="IS"/>
        <LinkedCondition Name="Heart attack" ICD="414.9" Type="IS"/>
        <LinkedCondition Name="Heart failure" ICD="428.0" Type="IS"/>
        <LinkedCondition Name="Mini stroke" ICD="435.9" Type="IS"/>
        <LinkedCondition Name="Stroke" ICD="434" Type="IS"/>
      </LinkedConditions>
      <ScreeningHistory>
        <DeclaredCondition Score="3.56">
          <conditions>
            <Condition>
              <id>1292</id>
              <parentid>-1</parentid>
              <name>Epilepsy</name>
              <questions>
                <Question>
                  <id>1</id>
                  <Text>If awake#$do you normally lose consciousness during a fit/seizure?</Text>
                  <currentAnswer>
                    <Text>Yes</Text>
                    <id>1</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>2</id>
                  <Text>How many fits/seizures causing loss of consciousness have you had in the last four weeks?</Text>
                  <currentAnswer>
                    <Text>0</Text>
                    <id>1</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>3</id>
                  <Text>How many fits/seizures causing loss of consciousness have you had in the last six months?</Text>
                  <currentAnswer>
                    <Text>0</Text>
                    <id>1</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>4</id>
                  <Text>How many unplanned hospital admissions have you had for epilepsy/seizures in the last year?</Text>
                  <currentAnswer>
                    <Text>1</Text>
                    <id>2</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>5</id>
                  <Text>How many different medicines do you take for your epilepsy/seizures?</Text>
                  <currentAnswer>
                    <Text>1</Text>
                    <id>2</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>6</id>
                  <Text>How long ago was your first fit/seizure?</Text>
                  <currentAnswer>
                    <Text>6 to 12 months ago</Text>
                    <id>2</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>7</id>
                  <Text>If not already declared to us#$is your epilepsy/seizures caused by:</Text>
                  <currentAnswer>
                    <Text>None of these</Text>
                    <id>4</id>
                  </currentAnswer>
                </Question>
              </questions>
              <currentQuestionId>7</currentQuestionId>
              <isAMTExclusion>false</isAMTExclusion>
              <isWSExclusion>false</isWSExclusion>
              <Score>3.56</Score>
              <ICD>345.9</ICD>
              <Deterioration>0</Deterioration>
              <isOkForWS>true</isOkForWS>
              <isOkForAMT>true</isOkForAMT>
              <exclusionType>None</exclusionType>
            </Condition>
          </conditions>
        </DeclaredCondition>
        <DeclaredCondition Score="1.45">
          <conditions>
            <Condition>
              <id>1332</id>
              <parentid>-1</parentid>
              <name>Blood pressure</name>
              <questions>
                <Question>
                  <id>1</id>
                  <Text>How many medicines does your doctor advise you to take for high blood pressure?</Text>
                  <currentAnswer>
                    <Text>1</Text>
                    <id>2</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>2</id>
                  <Text>Has your dose been increased or have you been prescribed a new tablet in the last six months?</Text>
                  <currentAnswer>
                    <Text>No</Text>
                    <id>2</id>
                  </currentAnswer>
                </Question>
                <Question>
                  <id>3</id>
                  <Text>Have you been advised to take a medication to lower your cholesterol level?</Text>
                  <currentAnswer>
                    <Text>No</Text>
                    <id>1</id>
                  </currentAnswer>
                </Question>
                <Question MQ="2">
                  <id>4</id>
                  <Text>Have you ever been a smoker?</Text>
                  <currentAnswer>
                    <Text>Yes - gave up less than a year ago</Text>
                    <id>3</id>
                  </currentAnswer>
                </Question>
              </questions>
              <currentQuestionId>4</currentQuestionId>
              <isAMTExclusion>false</isAMTExclusion>
              <isWSExclusion>false</isWSExclusion>
              <Score>1.45</Score>
              <ICD>401.9</ICD>
              <Deterioration>1</Deterioration>
              <isOkForWS>true</isOkForWS>
              <isOkForAMT>true</isOkForAMT>
              <exclusionType>None</exclusionType>
              <LinkedConditions>
                <LinkedCondition Name="High blood pressure" ICD="401.9" Type="D"/>
                <LinkedCondition Name="Renal failure" ICD="586" Type="D"/>
                <LinkedCondition Name="Abdominal aortic aneurysm" ICD="441.4" Type="I"/>
                <LinkedCondition Name="Peripheral vascular disease" ICD="443.9" Type="I"/>
                <LinkedCondition Name="Angina" ICD="414.9" Type="IS"/>
                <LinkedCondition Name="Enlarged heart" ICD="425" Type="IS"/>
                <LinkedCondition Name="Heart attack" ICD="414.9" Type="IS"/>
                <LinkedCondition Name="Heart failure" ICD="428.0" Type="IS"/>
                <LinkedCondition Name="Mini stroke" ICD="435.9" Type="IS"/>
                <LinkedCondition Name="Stroke" ICD="434" Type="IS"/>
              </LinkedConditions>
            </Condition>
          </conditions>
        </DeclaredCondition>
      </ScreeningHistory>
    </ScreeningPath>
  </ScreeningData>
</Screening>

xml全部在一个字段中提供,没有空格(我已经格式化了这个),并且在出现多个人的情况下,它显示为新的乘客记录.

我需要能够处理这个并提取类似的东西

旅客记录中的标题/姓氏/姓氏字段
然后从每个条件的每个答案重复从分支和与之相关的分支.

我觉得这很容易,但我很挣扎.

我到目前为止的守则……

第一个人是正确的,第二个人失去了一个兄弟姐妹?有没有错误或者是我吗?

StartItemNode:=XMLDoc.DocumentElement.ChildNodes.First;
ANode := StartItemNode;

repeat
  Title := ANode.ChildNodes['Title'].Text;
  Forename := ANode.ChildNodes['ForeName'].Text;
  Surname := ANode.ChildNodes['SurName'].Text;
  Age:=Anode.ChildNodes['Age'].Text;
  memo1.Lines.Add(Title+' '+Forename+' '+Surname+' '+Age);

  CNode:=Anode.ChildNodes.FindNode('Screening');
  CNode:=CNode.ChildNodes.FindNode('ScreeningData');
  CNode:=CNode.ChildNodes.FindNode('ScreeningPath');
  CNode:=CNode.ChildNodes.FindNode('ScreeningHistory');
  CNode:=Cnode.ChildNodes.FindNode('DeclaredCondition');
  CNode:=Cnode.ChildNodes.FindNode('conditions');
  CNode:=Cnode.ChildNodes.FindNode('Condition');
  (* Missing the 2nd illness on the 2nd node - but why *)

  repeat
   ill:=Cnode.ChildNodes['name'].text;
   memo1.Lines.add(ill);
   Unode:=Cnode;
   CNode:=Cnode.NextSibling;
  until cnode=nil;

如有任何帮助,请收到.
问候
菲尔

我们在旧版本的Delphi中广泛使用了TurboPower XML Partner. (不记得我们是否在Delphi 7中使用过它).这是SourceForge上最新版本的链接: http://sourceforge.net/projects/tpxmlpartner/.您可以找到适合您的先前版本.

您还可以使用msxml.dll中的xml com对象.它运作得很好.这里有一些关于它的更多信息:https://stackoverflow.com/a/1391413/758074.显然“位于XMLDoc.pas中的对象TXMLDocument(对于delphi 7)是MSXML 4或更少版本的包装器.”但是如果你导入版本6,你应该处于良好的状态.

网友评论