How to read and write XML using XDTO



1C:Enterprise platform integration capabilities and techniques

#1
People who like this:0Yes/0No
Active user
Rating: 7
Joined: Jul 28, 2015
Company:

I want to read a xml file. User click one button and choose a xml file. I want to getting that file and read it? How can I do? I created my XDTO object.

 
#2
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

Hello, Mehmet Tuğrul SARIÇİÇEK.

Here is an example on how to read and write XML using XDTO:

Code
&AtServer
Procedure WriteAtServer()
   
   TempFileName = GetTempFileName("xml");
   
   // Defining object types.
   ProductType = XDTOFactory.Type("http://www.sample-package.org", "Product");
   ProductPriceType = XDTOFactory.Type("http://www.sample-package.org", "ProductPrice");
   ProductPricesType = XDTOFactory.Type("http://www.sample-package.org", "ProductPrices");
   ProductPricesXDTO = XDTOFactory.Create(ProductPricesType);
   
   // Filling XDTO objects with data.
   For Each Row In ProductPrices Do
      ProductPriceXDTO = XDTOFactory.Create(ProductPriceType);
      ProductXDTO = XDTOFactory.Create(ProductType);
      Product = Row.Product.GetObject();
      ProductXDTO.Code = Product.Code;
      ProductXDTO.Description = Product.Description;
      ProductPriceXDTO.Product = ProductXDTO;
      ProductPriceXDTO.Price = Row.Price;
      ProductPricesXDTO.List.Add(ProductPriceXDTO);
   EndDo;
   
   // Creating XML.
   XMLWriter = New XMLWriter;
   XMLWriter.OpenFile(TempFileName);

   // Displaying XML.
   TextDocument = New TextDocument;
   TextDocument.Read(TempFileName);
   XML = TextDocument.GetText();

   DeleteFiles(TempFileName);
   
EndProcedure

&AtServer
Procedure ReadAtServer()
   
   TempFileName = GetTempFileName("xml");
   
   // Saving XML.
   TextDocument = New TextDocument;
   TextDocument.SetText(XML);
   TextDocument.Write(TempFileName);
   
   // Reading XML.
   XMLReader = New XMLReader;
   XMLReader.OpenFile(TempFileName);
   
   // Filling the list.
   ProductPrices.Clear();
   ProductPricesType = XDTOFactory.Type("http://www.sample-package.org", "ProductPrices");
   ProductPricesXDTO = XDTOFactory.ReadXML(XMLReader, ProductPricesType);
   For Each ProductPriceXDTO In ProductPricesXDTO.List Do
      ProductRef = Catalogs.Products.FindByCode(ProductPriceXDTO.Product.Code);
      If Not ValueIsFilled(ProductRef) Then
         ProductObject = Catalogs.Products.CreateItem();
         ProductObject.Code = ProductPriceXDTO.Product.Code;
         ProductObject.Description = ProductPriceXDTO.Product.Description;
         ProductObject.Write();
         ProductRef = ProductObject.Ref;
      EndIf;
      ProductPrice = ProductPrices.Add();
      ProductPrice.Product = ProductRef;
      ProductPrice.Price = ProductPriceXDTO.Price;
   EndDo;
   
   XMLReader.Close();
   
   DeleteFiles(TempFileName);
   
EndProcedure

The sample application is attached to this message.

Download xdto.png (20.46 KB)
Download 1Cv8.cf (11.69 KB)
 
#3
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

And here is an example that uses XMLSerializer object:

Code
&AtServer
Procedure WriteAtServer()
   
   TempFileName = GetTempFileName("xml");
   
   // Creating XML.
   XMLWriter = New XMLWriter;
   XMLWriter.OpenFile(TempFileName);
   // To get XDTO of another object you can use XDTO serializer:
   ProductPricesValue = FormAttributeToValue("ProductPrices");
   Serializer = New XDTOSerializer(XDTOFactory);
   ProductPricesXDTO = Serializer.WriteXDTO(ProductPricesValue);
   XDTOFactory.WriteXML(XMLWriter, ProductPricesXDTO);
   XMLWriter.Close();

   // Displaying XML.
   TextDocument = New TextDocument;
   TextDocument.Read(TempFileName);
   XML = TextDocument.GetText();

   DeleteFiles(TempFileName);
   
EndProcedure

&AtServer
Procedure ReadAtServer()
   
   TempFileName = GetTempFileName("xml");
   
   // Saving XML.
   TextDocument = New TextDocument;
   TextDocument.SetText(XML);
   TextDocument.Write(TempFileName);
   
   // Reading XML.
   XMLReader = New XMLReader;
   XMLReader.OpenFile(TempFileName);
   
   // Filling the list.
   // You can use XDTO serializer as well to read values.
   Serializer = New XDTOSerializer(XDTOFactory);
   ProductPricesValue = Serializer.ReadXML(XMLReader);
   ValueToFormAttribute(ProductPricesValue, "ProductPrices");
   
   XMLReader.Close();
   
   DeleteFiles(TempFileName);
   
EndProcedure

 
#4
People who like this:0Yes/0No
Active user
Rating: 7
Joined: Jul 28, 2015
Company:

Timofey Bugaevsky,

thank you so much sir

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)