Solved

How to create XML correctly?

Posted on 2013-01-18
4
428 Views
Last Modified: 2013-01-18
Hi,

I need to create following XML content:
		<SalesInvoiceList>
			<SalesInvoice ReferenceNumber="2006033">
				<ReferenceNumber>2006033</ReferenceNumber>
				<ContactPersonReference xsi:nil="true" />
				<CustomerReference ID="15" />
				<RecipientAddress>
					<Name>Zwembad Varik</Name>

Open in new window

I've come up with following code, but that doesn't have the correct result:
  // declared a private variables:
  Level0, Level1, Level2, Level3, Level4, Level5, Level6, Level7, Level8 : IXMLNode;

  Level3 := Level2.AddChild('SalesInvoice');
  Level3.Attributes['ReferenceNumber']                := QFacturenRlz.FieldByName('ReferenceNumber').AsString;
    Level4 := Level3.AddChild('ReferenceNumber');
    Level4.Text                                       := QFacturenRlz.FieldByName('ReferenceNumber').AsString;
    Level4.AddChild('ContactPersonReference');
    Level4.Attributes['xsi:nil']                      := 'true';
    Level4.AddChild('CustomerReference');
    Level4.Attributes['ID']                           := QFacturenRlz.FieldByName('ID').AsString;
    Level4.AddChild('RecipientAddress');
      Level5 := Level4.AddChild('Name');
      Level5.Text                                     := QFacturenRlz.FieldByName('FullName').AsString;

Open in new window

How can this be done correctly?
0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:Delphiwizard
ID: 38792917
I've read the examples, but it never goes to a level where it shows how to solve my question.
My code creates the following:
<SalesInvoiceList>
    <SalesInvoice ReferenceNumber="2">
        <ReferenceNumber ID="8884" xsi:nil="true">
        2
        <ContactPersonReference/>
        <CustomerReference/>
        <RecipientAddress/>
              <Name>
              Companyname

Open in new window


So it mixes up assigned values to nodes.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 38793616
It should go somehow like that...

uses XMLIntf,XMLDoc;

...

procedure TForm1.Button1Click(Sender: TObject);
 var
   Doc: IXMLDocument;
   Level1, Level2,Level3: IXMLNode;
begin
  Doc := NewXMLDocument;
  Level1 := Doc.AddChild('SalesInvoiceList');

  Level2 := Level1.AddChild('SalesInvoice');
  Level2.Attributes['ReferenceNumber']:='2006033';

  Level2.AddChild('ReferenceNumber');
  Level2.ChildValues['ReferenceNumber']:='2006033';

  Level3:=Level2.AddChild('ContactPersonReference');
  Level3.Attributes['xsi:nil']:='true';

  Level3:=Level2.AddChild('CustomerReference');
  Level3.Attributes['ID']:='15';

  Level3:=Level2.AddChild('RecipientAddress');
  Level3.AddChild('Name');
  Level3.ChildValues['Name']:='Zwembad Varik';

  Doc.SaveToFile('c:\temp\test.xml');
end;

Open in new window

0
 

Author Closing Comment

by:Delphiwizard
ID: 38793757
That does do the trick. Thank you very much for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month4 days, 17 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question