Ładowanie pliku XML do ListView

0

Witam

Szukam pomocy.

Chce załadować plik XML do ListView1.

ListView1->Items->Item[i]->Caption=Catalog->CD[i]->Get_TITLE(); 

ListView1->Items->Item[i]->SubItems->Add(Catalog->CD[i]->Get_ARTIST());

I to działa.
Niestety gdy w pliku brakuje np nazwy to dostaje błąd. Could not convert variant of type (null) into type (olestr)

Jak mogę to obejść.

2

Z tego pytania wiadomo tylko tyle, że masz xml i widok listy i na tym koniec.
jest jeszcze tag builder, który sugeruje C++Builder, ale nadal niw wiadomo o co chodzi.
Ty bardziej potrzebujesz jasnowidza lub telepaty, niż pomocy programisty.
Tragedia jak na kogoś zarejestrowanego 7 lat temu.

0
wojno napisał(a):

Niestety gdy w pliku brakuje np nazwy to dostaje błąd. […] Jak mogę to obejść.

Sprawdź czy nazwa istnieje. Wykonaj akcję gdy istnieje, w przeciwnym razie jej nie wykonuj, bo dostaniesz błąd.

0

Dzięki za zainteresowanie.

Poniżej zawartość pliku xml który działa.

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
  </CD>
</CATALOG>

Zawartość tego pliku nie działa.

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST></ARTIST>   <----------------------------------------------Tutaj nie ma nazwy
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
  </CD>
</CATALOG>
0

Dałeś xml-a, to jest krok do przodu.
Z googlania Could not convert variant of type (null) into type (olestr) wynika, że Get_TITLE() zwraca ci null. Z xml-a wynika, że to powinno zwracać pusty string.

Najwyraźniej jest problem z poprawnym parsowaniem xml-a, bo null powinien się pojawić jedynie w przypadku braku węzła ARTIST.
Czego używasz do parsowania tego Xml-a (jaka biblioteka)? Używasz kodu generowanego z wadl lub innej definicji formatu xml? Jeśli tak co to za generator jakiego wzorca używa?
Jeśli nie jest to kod generowany, to jak napisałeś klasy opisujące węzły xml-a, jak wygląda klasa reprezentująca CD i jak jest ona powiązana z parserem xml?

0

Użyłem komponentu XMLDocument z C++ Builder.
Tam jest taki Wizard jeśli go przejdziesz to dostaniesz plik jak poniżej.

Generalnie chciałem potestować przykład z youtube gdzie Gość prezentuje jak zaczytać dane z XML.
LINK

Plik z XML pobrałem gdzieś z Internetu.


#ifndef   cd_catalogH
#define   cd_catalogH

#include <System.hpp>
#include <Xml.Xmldom.hpp>
#include <Xml.XMLIntf.hpp>
#include <Xml.XMLDoc.hpp>
#include <XMLNodeImp.h>


// Forward Decls 

__interface IXMLCATALOGType;
typedef System::DelphiInterface<IXMLCATALOGType> _di_IXMLCATALOGType;
__interface IXMLCDType;
typedef System::DelphiInterface<IXMLCDType> _di_IXMLCDType;

// IXMLCATALOGType 

__interface INTERFACE_UUID("{7C861AE2-B0DE-4E2A-8FE7-DBE88969CEFD}") IXMLCATALOGType : public Xml::Xmlintf::IXMLNodeCollection
{
public:
public:
  // Property Accessors 
  virtual _di_IXMLCDType __fastcall Get_CD(int Index) = 0;
  // Methods & Properties 
  virtual _di_IXMLCDType __fastcall Add() = 0;
  virtual _di_IXMLCDType __fastcall Insert(const int Index) = 0;
  __property _di_IXMLCDType CD[int Index] = { read=Get_CD };/* default */
};

// IXMLCDType 

__interface INTERFACE_UUID("{78643A82-6907-4E2C-919B-7BF30402CC43}") IXMLCDType : public Xml::Xmlintf::IXMLNode
{
public:
  // Property Accessors 
  virtual System::UnicodeString __fastcall Get_TITLE() = 0;
  virtual System::UnicodeString __fastcall Get_ARTIST() = 0;
  virtual System::UnicodeString __fastcall Get_COUNTRY() = 0;
  virtual System::UnicodeString __fastcall Get_COMPANY() = 0;
  virtual System::UnicodeString __fastcall Get_PRICE() = 0;
  virtual int __fastcall Get_YEAR() = 0;
  virtual void __fastcall Set_TITLE(System::UnicodeString Value) = 0;
  virtual void __fastcall Set_ARTIST(System::UnicodeString Value) = 0;
  virtual void __fastcall Set_COUNTRY(System::UnicodeString Value) = 0;
  virtual void __fastcall Set_COMPANY(System::UnicodeString Value) = 0;
  virtual void __fastcall Set_PRICE(System::UnicodeString Value) = 0;
  virtual void __fastcall Set_YEAR(int Value) = 0;
  // Methods & Properties 
  __property System::UnicodeString TITLE = { read=Get_TITLE, write=Set_TITLE };
  __property System::UnicodeString ARTIST = { read=Get_ARTIST, write=Set_ARTIST };
  __property System::UnicodeString COUNTRY = { read=Get_COUNTRY, write=Set_COUNTRY };
  __property System::UnicodeString COMPANY = { read=Get_COMPANY, write=Set_COMPANY };
  __property System::UnicodeString PRICE = { read=Get_PRICE, write=Set_PRICE };
  __property int YEAR = { read=Get_YEAR, write=Set_YEAR };
};

// Forward Decls 

class TXMLCATALOGType;
class TXMLCDType;

// TXMLCATALOGType 

class TXMLCATALOGType : public Xml::Xmldoc::TXMLNodeCollection, public IXMLCATALOGType
{
  __IXMLNODECOLLECTION_IMPL__
protected:
  // IXMLCATALOGType 
  virtual _di_IXMLCDType __fastcall Get_CD(int Index);
  virtual _di_IXMLCDType __fastcall Add();
  virtual _di_IXMLCDType __fastcall Insert(const int Index);
public:
  virtual void __fastcall AfterConstruction(void);
};

// TXMLCDType 

class TXMLCDType : public Xml::Xmldoc::TXMLNode, public IXMLCDType
{
  __IXMLNODE_IMPL__
protected:
  // IXMLCDType 
  virtual System::UnicodeString __fastcall Get_TITLE();
  virtual System::UnicodeString __fastcall Get_ARTIST();
  virtual System::UnicodeString __fastcall Get_COUNTRY();
  virtual System::UnicodeString __fastcall Get_COMPANY();
  virtual System::UnicodeString __fastcall Get_PRICE();
  virtual int __fastcall Get_YEAR();
  virtual void __fastcall Set_TITLE(System::UnicodeString Value);
  virtual void __fastcall Set_ARTIST(System::UnicodeString Value);
  virtual void __fastcall Set_COUNTRY(System::UnicodeString Value);
  virtual void __fastcall Set_COMPANY(System::UnicodeString Value);
  virtual void __fastcall Set_PRICE(System::UnicodeString Value);
  virtual void __fastcall Set_YEAR(int Value);
};

// Global Functions 

_di_IXMLCATALOGType __fastcall GetCATALOG(Xml::Xmlintf::_di_IXMLDocument Doc);
_di_IXMLCATALOGType __fastcall GetCATALOG(Xml::Xmldoc::TXMLDocument *Doc);
_di_IXMLCATALOGType __fastcall LoadCATALOG(const System::UnicodeString& FileName);
_di_IXMLCATALOGType __fastcall  NewCATALOG();

#define TargetNamespace ""

#endif

i

#include <System.hpp>
#pragma hdrstop

#include "cd_catalog.h"


// Global Functions 

_di_IXMLCATALOGType __fastcall GetCATALOG(Xml::Xmlintf::_di_IXMLDocument Doc)
{
  return (_di_IXMLCATALOGType) Doc->GetDocBinding("CATALOG", __classid(TXMLCATALOGType), TargetNamespace);
};

_di_IXMLCATALOGType __fastcall GetCATALOG(Xml::Xmldoc::TXMLDocument *Doc)
{
  Xml::Xmlintf::_di_IXMLDocument DocIntf;
  Doc->GetInterface(DocIntf);
  return GetCATALOG(DocIntf);
};

_di_IXMLCATALOGType __fastcall LoadCATALOG(const System::UnicodeString& FileName)
{
  return (_di_IXMLCATALOGType) Xml::Xmldoc::LoadXMLDocument(FileName)->GetDocBinding("CATALOG", __classid(TXMLCATALOGType), TargetNamespace);
};

_di_IXMLCATALOGType __fastcall  NewCATALOG()
{
  return (_di_IXMLCATALOGType) Xml::Xmldoc::NewXMLDocument()->GetDocBinding("CATALOG", __classid(TXMLCATALOGType), TargetNamespace);
};

// TXMLCATALOGType 

void __fastcall TXMLCATALOGType::AfterConstruction(void)
{
  RegisterChildNode(System::UnicodeString("CD"), __classid(TXMLCDType));
  ItemTag = "CD";
  ItemInterface = __uuidof(IXMLCDType);
  Xml::Xmldoc::TXMLNodeCollection::AfterConstruction();
};

_di_IXMLCDType __fastcall TXMLCATALOGType::Get_CD(int Index)
{
  return (_di_IXMLCDType) List->Nodes[Index];
};

_di_IXMLCDType __fastcall TXMLCATALOGType::Add()
{
  return (_di_IXMLCDType) AddItem(-1);
};

_di_IXMLCDType __fastcall TXMLCATALOGType::Insert(const int Index)
{
  return (_di_IXMLCDType) AddItem(Index);
};

// TXMLCDType 

System::UnicodeString __fastcall TXMLCDType::Get_TITLE()
{
  return GetChildNodes()->Nodes[System::UnicodeString("TITLE")]->NodeValue.operator System::UnicodeString();
};

void __fastcall TXMLCDType::Set_TITLE(System::UnicodeString Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("TITLE")]->NodeValue = Value;
};

System::UnicodeString __fastcall TXMLCDType::Get_ARTIST()
{
  return GetChildNodes()->Nodes[System::UnicodeString("ARTIST")]->NodeValue.operator System::UnicodeString();
};

void __fastcall TXMLCDType::Set_ARTIST(System::UnicodeString Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("ARTIST")]->NodeValue = Value;
};

System::UnicodeString __fastcall TXMLCDType::Get_COUNTRY()
{
  return GetChildNodes()->Nodes[System::UnicodeString("COUNTRY")]->NodeValue.operator System::UnicodeString();
};

void __fastcall TXMLCDType::Set_COUNTRY(System::UnicodeString Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("COUNTRY")]->NodeValue = Value;
};

System::UnicodeString __fastcall TXMLCDType::Get_COMPANY()
{
  return GetChildNodes()->Nodes[System::UnicodeString("COMPANY")]->NodeValue.operator System::UnicodeString();
};

void __fastcall TXMLCDType::Set_COMPANY(System::UnicodeString Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("COMPANY")]->NodeValue = Value;
};

System::UnicodeString __fastcall TXMLCDType::Get_PRICE()
{
  return GetChildNodes()->Nodes[System::UnicodeString("PRICE")]->NodeValue.operator System::UnicodeString();
};

void __fastcall TXMLCDType::Set_PRICE(System::UnicodeString Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("PRICE")]->NodeValue = Value;
};

int __fastcall TXMLCDType::Get_YEAR()
{
  return GetChildNodes()->Nodes[System::UnicodeString("YEAR")]->NodeValue.operator int();
};

void __fastcall TXMLCDType::Set_YEAR(int Value)
{
  GetChildNodes()->Nodes[System::UnicodeString("YEAR")]->NodeValue = Value;
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1