XML操作,读属性

<?xml version="1.0" encoding="GB2312"?>
<Root Alias="部标状态" TerminalType="808">    
  <Status desc="车辆状态位信息定义" TestBitValue="1" InOverBoundAlarm="4,4" OutOverBoundAlarm="8,7" >
      <Byte seq="1" desc="报警第1状态字" >
          <Bit seq="0"    alarm="1"    value1="紧急报警"   s13nValue1=""/>
          <Bit seq="1"    alarm="0"    value1="超速报警"   s13nValue1=""/>
          <Bit seq="2"    alarm="0"    value1="疲劳驾驶报警"   s13nValue1=""/>    
                <Bit seq="3"    alarm="0"    value1="预警"   s13nValue1=""/>
                <Bit seq="4"    alarm="1"    value1="GNSS模块故障报警"   s13nValue1=""/>
                <Bit seq="5"    alarm="1"    value1="GNSS模块断开报警"   s13nValue1=""/>
                <Bit seq="6"    alarm="1"    value1="GNSS模块短路报警"   s13nValue1=""/>
                <Bit seq="7"    alarm="1"    value1="主电源欠压报警"   s13nValue1=""/>
        </Byte>
      <Byte seq="2" desc="报警第2状态字" >
                <Bit seq="0"    alarm="1"    value1="主电源掉电报警"   s13nValue1=""/>
                <Bit seq="1"    alarm="0"    value1="LCD模块故障报警"   s13nValue1=""/>
                <Bit seq="2"    alarm="0"    value1="TTS模块故障报警"   s13nValue1=""/>
                <Bit seq="3"    alarm="0"    value1="摄像头故障报警"   s13nValue1=""/>
                <Bit seq="4"    alarm="0"    value1=""   s13nValue1=""/>
                <Bit seq="5"    alarm="0"    value1=""   s13nValue1=""/>
                <Bit seq="6"    alarm="0"    value1=""   s13nValue1=""/>
                <Bit seq="7"    alarm="0"    value1=""   s13nValue1=""/>
        </Byte>
    <Byte seq="8" desc="第4状态字" >
                <Bit seq="0"    alarm="0"    value1=""    s13nValue1=""/>
                <Bit seq="1"    alarm="0"    value1=""    s13nValue1=""/>
                <Bit seq="2"    alarm="0"    value1=""    s13nValue1=""/>
                <Bit seq="3"    alarm="0"    value1=""    s13nValue1=""/>
                <Bit seq="4"    alarm="0"    value1="蜂鸣器开"    value0="蜂鸣器关" s13nValue1="" s13nValue0=""/>
                <Bit seq="5"    alarm="0"    value1="设防状态"    value0="不设防状态" s13nValue1="" s13nValue0=""/>
                <Bit seq="6"    alarm="0"    value1=""    s13nValue1=""/>
                <Bit seq="7"    alarm="0"    value1=""    s13nValue1=""/>
       </Byte>
  </Status>
  <!-- end of status define-->
</Root>

 

//加载xml
TalarmTypeList = record
      AlarmName : string;
      end;
  pAlarmTypeList =^ TalarmTypeList;
//1、加载xml
  if not FileExists(currentDirectory + filename) then
  xmlDoc := TXMLDocument.Create(currentDirectory + FileName);

//2、使用
procedure TfrmConfigAlarm.loadStatus;
var
  RootNode      : IXMLNode;
  StatusNode    : IXMLNode;
  ByteNode      : IXMLNode;
  BitNode       : IXMLNode;
  Attributes    : Variant; //因为返回的是 OleVariant类型
  I             : Integer;
  J             : Integer;
  ByteIndex     : Integer;
  BitIndex      : Integer;
  RequireList   : TStringList;
  minByteIndex  : Integer;
  maxByteIndex  : Integer;
  minBitIndex   : Integer;
  maxBitIndex   : Integer;
  node          : PVirtualNode;
  data          : pAlarmTypeList;
  alarm,value0,value1 : Variant;
begin
  vstAlarmList.Clear;
  xmlDoc.Active := True;//激活
  RootNode := xmlDoc.ChildNodes.Nodes['Root'];//取出第一个节点
  if RootNode = nil then
  begin
    ShowMessage('读取配置文件失败');
    Exit;
  end;
  StatusNode := RootNode.ChildNodes.FindNode('Status'); //取出第二个节点
  if StatusNode = nil then
  begin
    ShowMessage('读取配置文件失败');
    Exit;
  end;
  for I := 0 to StatusNode.ChildNodes.Count - 1 do //Status 地下的子节点
  begin
    if I > highByteIndex then
      Break;

    ByteNode := StatusNode.ChildNodes.Get(I);
    Attributes := ByteNode.Attributes['seq']; //取属性
    for J := 0 to ByteNode.ChildNodes.Count - 1 do //再下一层子节点
    begin
      if J > 8 then
        Break;

      BitNode := ByteNode.ChildNodes.Get(J);

      Attributes := BitNode.Attributes['seq'];
      if Attributes <> Null then
        BitIndex := Attributes
      else
        BitIndex := J;
      if BitIndex < minBitIndex then
        minBitIndex := BitIndex;
      if BitIndex > maxBitIndex then
        maxBitIndex := BitIndex;
      alarm := BitNode.Attributes['alarm']; //取属性
      value1 := BitNode.Attributes['value1'];
      value0 := BitNode.Attributes['value0'];
      if (value0<>null) and (value0<>'') then
      begin
        node := vstAlarmList.AddChild(nil);//添加一行
        data := vstAlarmList.GetNodeData(node);//获取虚拟树的行内存
        data.AlarmName := value0;
        node.CheckType := ctCheckBox; //选择框
        vstAlarmList.TreeOptions.MiscOptions := vstAlarmList.TreeOptions.MiscOptions+[toCheckSupport];
        if alarm > 0 then //是否勾选
          node.CheckState := csCheckedNormal else
          node.CheckState := csUncheckedNormal;
      end;
    //。。。。。省略
    end;
  end;
end;
//3、勾选后的,取到列表里面
procedure TfrmConfigAlarm.LoadSelectalarm;
var
  node : PVirtualNode;
  data : pAlarmTypeList;
  i    : Integer;
begin
  listSelectAlarm.Clear;
  node := vstAlarmList.GetFirstChecked();//得到第一个 勾选的值,  虚拟树,勾选只可以在第一列
  while Assigned(node) do
  begin
    data := vstAlarmList.GetNodeData(node);
    listSelectAlarm.Add(data.AlarmName);  //放Tstringlist里面
    node := vstAlarmList.GetNextChecked(node);
  end;
end;

 

posted @ 2016-10-18 17:46  海蓝7  阅读(166)  评论(0编辑  收藏  举报