delphi 7读取和处理xml文件如何以及哪个组件 - 更新

发布于 2024-12-29 09:38:35 字数 10277 浏览 1 评论 0原文

我有一个客户提供一个包含混合逗号分隔数据和 xml 的文件。逗号分隔不是问题,但 xml 对我来说是全新的。

我试图找到一个组件来完成我需要的操作(omnixml -放弃 - 使用 delphi 内置 xml 组件)似乎是可能的...

我有如下数据:

<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 全部在一个字段中提供,没有空格(我已对其进行格式化),并且如果有多个人,则显示为新的乘客记录。

我需要能够处理这个问题并

从乘客记录中提取诸如标题/名字/姓氏字段之类 的内容 然后从分支和与它相关的和对于每个条件的每个答案重复。

我认为这很容易,但我很挣扎。

到目前为止我拥有的代码....

第一个人是正确的,第二个人失去了一个兄弟姐妹?是有bug还是我的问题?

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;

如有任何帮助,我们将不胜感激。 问候 菲尔

I have a client who is supplying a file with mixed comma seperated data and xml. The comma seperated is not a problem but the xml is all new to me.

I have tried to find a component to do what i need (omnixml -abandoned - using delphi built in xml component) seems possible...

I have data like the following :

<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>

The xml is all supplied in one field no spaces (i have formatted this) and where there is more than one person it appears this appears as a new passenger record.

I need to be able to process this and extract things like

Title/Forename/Surname fields from the passenger record
and then from the branch and from the and which it relates to that is repeated for each answer to each condition.

I think it is fairly easy but I am struggling.

Code I have so far....

The first person is correct the 2nd one looses one of its siblings? Is there a bug or is it me?

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;

Any help would be grateful received please.
Regards
Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

香草可樂 2025-01-05 09:38:35

我们在旧版本的 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 o 以下版本的包装器。”。但如果你导入版本6,你应该处于良好状态。

We have used TurboPower XML Partner extensively in older versions of Delphi. (Cannot remember if we used it in Delphi 7). Here is a link to the latest version on SourceForge: http://sourceforge.net/projects/tpxmlpartner/. You may be able to find a previous version that will work for you.

You can also use the xml com object found in msxml.dll. It works quite well. Here is some more information about it: https://stackoverflow.com/a/1391413/758074. Apparently "The object TXMLDocument (for delphi 7) located in XMLDoc.pas is a Wrapper of the version MSXML 4 o less.". But if you import version 6, you should be in good shape.

呆橘 2025-01-05 09:38:35

您不需要单独的库:Delphi 已经提供了一个

通过使用XML 数据绑定向导为您的(未格式化的)XML 文件创建一个接口单元(仅适用于企业版中的 D7),参见文件>新的>其他>新的> XML 数据绑定。。根据您的喜好进行调整,但默认情况下,只需单击“确定”传递每个向导页面就可以了。 (请注意,其他 Delphi 版本的默认设置可能与我的不同。)尽管我个人喜欢删除的一件事是每个接口类型的“Type”后缀。 (以及类类型名称,但这不是向导中的选项,因此您可以手动执行此操作。)

请参阅 此答案 作为示例或了解更多说明。

请注意,此向导只是为了方便:您也可以单独使用 TXMLDocument(在组件选项板的 Internet 选项卡上)。通过 ADocument.DocumentElement 获取根节点,通过 Node.ChildNodes.FindNode('Node name') 获取子节点,通过 Node.ChildValues[' 获取其值值名称']

You do not need a separate library: Delphi goes with one already

Create an interface unit for your (unfomatted) XML file by using the XML Data Binding Wizard (for D7 only in enterprise edition), see File > New > Other > New > XML Data Binding. Tweak as you like, but simply passing every wizard page by clicking OK works just fine by default. (Note that the default settings for other Delphi versions might differ from that of mine.) Though the one thing I personally like to get rid of is the "Type" suffix for every interface type. (As well as for the class type names, but that's not an option in the wizard, so you might do that manually.)

See this answer as example or for more instructions.

Note that this wizard is only for convenience: you could also do solely with TXMLDocument (on tab Internet of the component palette). Obtain the root node with ADocument.DocumentElement, obtain child nodes with Node.ChildNodes.FindNode('Node name') and its values with Node.ChildValues['Value name'].

相思碎 2025-01-05 09:38:35

我真的很喜欢使用 SimDesign 的 NativeXML。它有不错的文档并且是开源的。
http://www.simdesign.nl/nativexml.html

I'm really enjoying using NativeXML by SimDesign. It has decent docs and is open source.
http://www.simdesign.nl/nativexml.html

森末i 2025-01-05 09:38:35

对于 TXmlDocument,Msxml 内置在 D7 中(或者我正在使用的 D6,大致相同)。

还有一个旧版本的 Dieter Köhler 内置的 XDOM,或者您可以从 www.philo.de/xml/ 下载他更新的 ADOM(使用来自 github.com/Midiar/adomxmldom 的 IDomNode 包装器)。这些都是用 Pascal 实现的 - 非常适合调试。 ADOM 为您提供 XPath 功能。

编辑:Msxml 也有 XPath(以及 XSLT)。

For TXmlDocument, Msxml is built-in in D7 (or D6 as I'm using, roughly the same).

There is also an old version of Dieter Köhler's XDOM built-in, or you could download his updated ADOM from www.philo.de/xml/ (with the IDomNode wrapper from github.com/Midiar/adomxmldom). Those are implemented in Pascal - nice for debugging. ADOM gives you XPath functionality.

Edit: Msxml also has XPath (as well as XSLT).

书信已泛黄 2025-01-05 09:38:35

我确实解决了这个问题并使用了 Delphi7 内置的 XML 对象。
并且适合多人。

这比我最初想象的要困难得多。

正如上面的结构,我发现如果您声明了某种健康状况,一切都很好,但如果暗示该状况,例如您患有高血压,因此您可能会升高胆固醇(拼写!),那么后续问题不在主要声明的条件下分支但有条件。

下面的代码确实适用于此 - 有很大一部分重复引用,但我希望它能够工作而不是看起来很漂亮..

而且我必须承认我对 XML 的了解比我想要的还要多,哈哈...还有很大的感谢: -) 感谢所提供的所有帮助 - 大量阅读堆栈溢出和一些谷歌搜索,我现在有了结果!

procedure TForm1.Button1Click(Sender: TObject);
var
 StartItemNode:IXMLNode;
 ANode:IXMLNode;
 CNode:IXMLNode;
 LNode:IXMLNode;
 QNode:IXMLNode;
 UNode:IXMLNode;
 hnode:ixmlnode;
INode:IXMLNode;
number:widestring;  
Title:widestring;
Forename:widestring;
Surname:widestring;
Age:widestring;
ill:widestring;
quest:widestring;
answer:widestring;
first:boolean;
who:string;
begin
 opendialog1.Execute;
 who:=extractfilepath(opendialog1.FileName)+extractfilename(opendialog1.filename)
 +'_TXT.TXT';
 XMLDOC.FileName:=opendialog1.FileName;

 XMLDoc.Active:=True;

memo1.Clear;
XMLDOC.SaveToFile('c:\philxx.xml');

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

memo1.Lines.add('Filename '+opendialog1.FileName);
if anode<>nil then
begin

repeat
  memo1.Lines.Add('====== New Person ======');
  number :=anode.childnodes['number'].Text;
  Title := ANode.ChildNodes['Title'].Text;
  Forename := ANode.ChildNodes['ForeName'].Text;
  Surname := ANode.ChildNodes['SurName'].Text;
  Age:=Anode.ChildNodes['Age'].Text;
  memo1.lines.add(number);
  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');

  HNode:=Cnode.ChildNodes.FindNode('DeclaredCondition');
  while hnode<>nil do
  begin
    unode:=hnode;
  memo1.lines.add('==== Illnesses ===');
  if unode<>nil then
  begin
   UNode:=Unode.ChildNodes.FindNode('conditions');
   Cnode:=Unode;
   Inode:=CNode.ChildNodes.FindNode('Condition');
  while unode<>nil do
  begin
   ill:=Inode.ChildNodes['name'].text;
   memo1.Lines.add(ill);
   lnode:=Unode;
   unode:=unode.nextsibling;
   if unode=nil then
    begin
     unode:=lnode;
     inode:=inode.NextSibling;
     if inode=nil then unode:=nil;
    end
    else
    begin
     CNode:=Unode.ChildNodes.FindNode('conditions');
     Inode:=CNode.ChildNodes.FindNode('Condition');
    end;


  end;

  end;
   hnode:=hnode.nextsibling;
  end;
  CNode:=Anode.ChildNodes.FindNode('Screening');
  CNode:=CNode.ChildNodes.FindNode('ScreeningData');
  CNode:=CNode.ChildNodes.FindNode('ScreeningPath');
  CNode:=CNode.ChildNodes.FindNode('ScreeningHistory');
  HNode:=Cnode.ChildNodes.FindNode('DeclaredCondition');


  while hnode<>nil do
  begin
    unode:=hnode;

  (* Gets all the questions but seems to have same issue with missing questions for 2nd illness *)
  if unode<>nil then
  begin
   UNode:=Unode.ChildNodes.FindNode('conditions');
   Cnode:=Unode;
   Inode:=CNode.ChildNodes.FindNode('Condition');



  while unode<>nil do
   begin
     QNode:=inode.ChildNodes.FindNode('questions');
     if qnode=nil then
      begin
       memo1.lines.add('=== No questions for Illness === ');
      end;
     if qnode<>nil then
      begin
        qNode:=qNode.ChildNodes.FindNode('Question');
        memo1.Lines.add('==== New Questions ====');
        while qnode<>nil do
         begin
          quest:=qnode.ChildNodes['Text'].text;
          memo1.Lines.add(quest);
          answer:=qnode.ChildNodes['currentAnswer'].ChildNodes['Text'].text;
          memo1.lines.add(answer);
          qNode:=qnode.NextSibling;
         end;
      end;
     lnode:=unode;
     unode:=unode.NextSibling;
     if unode=nil then
     begin
       unode:=lnode;
       inode:=inode.NextSibling;
       if inode=nil then unode:=nil;
     end
     else
     begin
      CNode:=Unode.ChildNodes.FindNode('conditions');
      Inode:=CNode.ChildNodes.FindNode('Condition');
     end;
   end;
  end;
   hnode:=hnode.nextsibling;
  end;

  ANode := ANode.NextSibling;
until ANode = nil;
end
  else memo1.lines.Add('No Illness or Questions Found ');
  memo1.Lines.SaveToFile(who);
  showmessage('done');

I did fix the problem and used Delphi7 built in XML object.
And for multiple people.

It was alot harder than I first thought.

As on the structure above I found if you declared a medical condition all was well but if the condition is implied e.g you have high blood pressure so therefore you may have raised cholesteral (spelling!) then the subsequent question are not under the main declared conditions branch but under conditions.

The code following does work for this - there is a large part of repeated quote BUT I wanted it to work rather than look pretty..

And I must admit i have learnt more about XML than I wanted to lol... And big thaks :-) to all the help that was offered - a lot of reading on stack overflow and some google searching and I now have a result!

procedure TForm1.Button1Click(Sender: TObject);
var
 StartItemNode:IXMLNode;
 ANode:IXMLNode;
 CNode:IXMLNode;
 LNode:IXMLNode;
 QNode:IXMLNode;
 UNode:IXMLNode;
 hnode:ixmlnode;
INode:IXMLNode;
number:widestring;  
Title:widestring;
Forename:widestring;
Surname:widestring;
Age:widestring;
ill:widestring;
quest:widestring;
answer:widestring;
first:boolean;
who:string;
begin
 opendialog1.Execute;
 who:=extractfilepath(opendialog1.FileName)+extractfilename(opendialog1.filename)
 +'_TXT.TXT';
 XMLDOC.FileName:=opendialog1.FileName;

 XMLDoc.Active:=True;

memo1.Clear;
XMLDOC.SaveToFile('c:\philxx.xml');

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

memo1.Lines.add('Filename '+opendialog1.FileName);
if anode<>nil then
begin

repeat
  memo1.Lines.Add('====== New Person ======');
  number :=anode.childnodes['number'].Text;
  Title := ANode.ChildNodes['Title'].Text;
  Forename := ANode.ChildNodes['ForeName'].Text;
  Surname := ANode.ChildNodes['SurName'].Text;
  Age:=Anode.ChildNodes['Age'].Text;
  memo1.lines.add(number);
  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');

  HNode:=Cnode.ChildNodes.FindNode('DeclaredCondition');
  while hnode<>nil do
  begin
    unode:=hnode;
  memo1.lines.add('==== Illnesses ===');
  if unode<>nil then
  begin
   UNode:=Unode.ChildNodes.FindNode('conditions');
   Cnode:=Unode;
   Inode:=CNode.ChildNodes.FindNode('Condition');
  while unode<>nil do
  begin
   ill:=Inode.ChildNodes['name'].text;
   memo1.Lines.add(ill);
   lnode:=Unode;
   unode:=unode.nextsibling;
   if unode=nil then
    begin
     unode:=lnode;
     inode:=inode.NextSibling;
     if inode=nil then unode:=nil;
    end
    else
    begin
     CNode:=Unode.ChildNodes.FindNode('conditions');
     Inode:=CNode.ChildNodes.FindNode('Condition');
    end;


  end;

  end;
   hnode:=hnode.nextsibling;
  end;
  CNode:=Anode.ChildNodes.FindNode('Screening');
  CNode:=CNode.ChildNodes.FindNode('ScreeningData');
  CNode:=CNode.ChildNodes.FindNode('ScreeningPath');
  CNode:=CNode.ChildNodes.FindNode('ScreeningHistory');
  HNode:=Cnode.ChildNodes.FindNode('DeclaredCondition');


  while hnode<>nil do
  begin
    unode:=hnode;

  (* Gets all the questions but seems to have same issue with missing questions for 2nd illness *)
  if unode<>nil then
  begin
   UNode:=Unode.ChildNodes.FindNode('conditions');
   Cnode:=Unode;
   Inode:=CNode.ChildNodes.FindNode('Condition');



  while unode<>nil do
   begin
     QNode:=inode.ChildNodes.FindNode('questions');
     if qnode=nil then
      begin
       memo1.lines.add('=== No questions for Illness === ');
      end;
     if qnode<>nil then
      begin
        qNode:=qNode.ChildNodes.FindNode('Question');
        memo1.Lines.add('==== New Questions ====');
        while qnode<>nil do
         begin
          quest:=qnode.ChildNodes['Text'].text;
          memo1.Lines.add(quest);
          answer:=qnode.ChildNodes['currentAnswer'].ChildNodes['Text'].text;
          memo1.lines.add(answer);
          qNode:=qnode.NextSibling;
         end;
      end;
     lnode:=unode;
     unode:=unode.NextSibling;
     if unode=nil then
     begin
       unode:=lnode;
       inode:=inode.NextSibling;
       if inode=nil then unode:=nil;
     end
     else
     begin
      CNode:=Unode.ChildNodes.FindNode('conditions');
      Inode:=CNode.ChildNodes.FindNode('Condition');
     end;
   end;
  end;
   hnode:=hnode.nextsibling;
  end;

  ANode := ANode.NextSibling;
until ANode = nil;
end
  else memo1.lines.Add('No Illness or Questions Found ');
  memo1.Lines.SaveToFile(who);
  showmessage('done');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文