CheckListbox和xml

时间:2017-04-28 18:12:02

标签: xml delphi

TCheckListBox中,我加载了Category.Type和Country.Code(XML文件):

procedure TForm1.btnLoadXMLClick(Sender: TObject);
var
  I: Integer;
  lCategory: IXMLCategoryType;
  ICountry:  IXMLCountryType;

begin
  if self.OpenDialog1.Execute then
  begin
    self.XMLDocument1.Active := true;
    self.XMLDocument1.loadFromfile(self.OpenDialog1.FileName);
    XMLIntf := GetDictionary(self.XMLDocument1);    
    CheckListBox1.Items.Clear;
    CheckListBox2.Items.Clear;

    for I := 0 to XMLIntf.Categories.Count - 1 do
    begin
      lCategory := XMLIntf.Categories.Category[I];
      self.CheckListBox1.Items.Add(lCategory.Type);
    end;

   for I := 0 to XMLIntf.Languages.Count - 1 do
   begin
      lCountry := XMLIntf.Countries.Country[I];
      self.CheckListBox2.Items.Add(lCountry.Code);
   end;

如何在CarBrand中检查的每个类别中为TListBox正确添加名为TCheckListBox的XML元素?代码应该怎么样?

procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);
var
  I: Integer;
  lCategory: IXMLCategoryType;
  ICar:     IXMLWordType;
begin
  ListBox1.Items.BeginUpdate();
  ListBox1.Items.Clear();
  try
    ICar := XMLIntf.Cars.Car[I];
    lCategory := XMLIntf.Categories.Category[I] ;       
    for I := 0 to CheckListBox1.Items.Count - 1 do
      if CheckListBox1.Checked[I] then
      begin
        if lCategory[i].Id = ICar[i].CategoryId then
          self.ListBox1.Items.Add(ICar.CarBrand);
      end;
  finally
    ListBox1.Items.EndUpdate();
  end;
end;

XML

   <?xml version="1.0"?>

    <transport>
      <Countries>
        <Country>
          <code>SWE</code>
       </Country>
       <Country>
          <code>ITA</code>
       </Country>
      </Countries>

     <Categories>
       <Category>
         <id>1</id>
         <type>Coupe</type>
      </Category>
      <Category>
         <id>2</id>
         <type>Truck</type>
     </Category>
    </Categories>


     <Cars>
       <Car>
        <CountryId>ITA</CountryId>
        <CategoryId>1</CategoryId>
        <CarBrand>Alfa Romeo</CarBrand>
        <photo>C:\alfa_romeo.jpg</photo>
        </Car>
       <Car>
         <CountryId>SWE</CountryId>
         <CategoryId>2</CategoryId>
         <CarBrand>Volvo</CarBrand>
         <photo>C:\volvo.jpg</photo>
      </Car>
    </Cars>
  </transport>

1 个答案:

答案 0 :(得分:2)

CheckListBox1ClickCheck()中,您在访问IICar之前未将lCategory设置为任何内容。

尝试更像这样的东西:

procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);
var
  I, J: Integer;
  ICar: IXMLWordType;
  Value: String;
begin
  ListBox1.Items.BeginUpdate;
  try
    ListBox1.Items.Clear;

    for I := 0 to XMLIntf.Cars.Count - 1 do
    begin
      ICar := XMLIntf.Cars.Car[I];
      Value := ICar.CategoryId;

      for J := 0 to CheckListBox1.Items.Count - 1 do
      being
        if CheckListBox1.Checked[J] then
        begin
          if XMLIntf.Categories.Category[J].Id = Value then
          begin
            self.ListBox1.Items.Add(ICar.CarBrand);
            Break;
          end;
        end;
      end;
    end;
  finally
    ListBox1.Items.EndUpdate;
  end;
end;

可替换地:

procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);
var
  I: Integer;
  ICar: IXMLWordType;
  CategoryIDs: TStringList;
begin
  ListBox1.Items.BeginUpdate;
  try
    ListBox1.Items.Clear;

    CategoryIDs := TStringList.Create;
    try
      for I := 0 to CheckListBox1.Items.Count - 1 do
      begin
        if CheckListBox1.Checked[I] then
          CategoryIDs.Add(XMLIntf.Categories.Category[I].Id);
      end;

      for I := 0 to XMLIntf.Cars.Count - 1 do
      begin
        ICar := XMLIntf.Cars.Car[I];
        if CategoryIDs.IndexOf(ICar.CategoryId) <> -1 then
          self.ListBox1.Items.Add(ICar.CarBrand);
      end;
    finally
      CategoryIDs.Free;
    end;
  finally
    ListBox1.Items.EndUpdate;
  end;
end;

更新:要将CountryCode添加到混音中,您可以执行以下操作:

// assign this single handler to the OnClickCheck event
// for BOTH CheckListBox1 and CheckListBox2...

procedure TForm1.CheckListBoxClickCheck(Sender: TObject);
var
  I, J: Integer;
  ICar: IXMLWordType;
  Value: string;
  Found: Boolean;
begin
  ListBox1.Items.BeginUpdate;
  try
    ListBox1.Items.Clear;

    for I := 0 to XMLIntf.Cars.Count - 1 do
    begin
      ICar := XMLIntf.Cars.Car[I];

      Value := ICar.CategoryId;
      Found := False;

      for J := 0 to CheckListBox1.Items.Count - 1 do
      begin
        if CheckListBox1.Checked[J] then
        begin
          if XMLIntf.Categories.Category[J].Id = Value then
          begin
            Found := True;
            Break;
          end;
        end;
      end;

      if not Found then
        Continue;

      Value := iCar.CountryCode;
      Found := False;

      for J := 0 to CheckListBox2.Items.Count - 1 do
      being
        if CheckListBox2.Checked[J] then
        begin
          if XMLIntf.Countries.Country[J].Code = Value then
          begin
            Found := True;
            Break;
          end;
        end;
      end;

      if not Found then
        Continue;

      self.ListBox1.Items.Add(ICar.CarBrand);
    end;
  finally
    ListBox1.Items.EndUpdate;
  end;
end;

可替换地:

procedure TForm1.CheckListBoxClickCheck(Sender: TObject);
var
  I: Integer;
  ICar: IXMLWordType;
  CategoryIDs: TStringList;
  CountryCodes: TStringList;
begin
  ListBox1.Items.BeginUpdate;
  try
    ListBox1.Items.Clear;

    CategoryIDs := TStringList.Create;
    try
      CountryCodes := TStringList.Create;

      for I := 0 to CheckListBox1.Items.Count - 1 do
      being
        if CheckListBox1.Checked[I] then
          CategoryIDs.Add(XMLIntf.Categories.Category[I].Id);
      end;

      for I := 0 to CheckListBox2.Items.Count - 1 do
      being
        if CheckListBox2.Checked[I] then
          CountryCodes.Add(XMLIntf.Countries.Country[I].Code);
      end;

      for I := 0 to XMLIntf.Cars.Count - 1 do
      begin
        ICar := XMLIntf.Cars.Car[I];

        if (CategoryIDs.IndexOf(ICar.CategoryId) <> -1) and
           (CountryCodes.IndexOf(ICar.CountryCode) <> -1) then
        begin
          self.ListBox1.Items.Add(ICar.CarBrand);
        end;
      finally
        CountryCodes.Free;
      end;
    finally
      CategoryIDs.Free;
    end;
  finally
    ListBox1.Items.EndUpdate();
  end;
end;