-2

Okay, so I'm now aware of the fact I need to use TXMLDocument to open a .xml in Delphi 7, but I had a few further questions (hopefully answerable ones that haven't already been answered, I didn't see anything that looked overly helpful)

  • If I run tempdoc.LoadFromFile('document.xml'); - just as an example, it throws up an Access Violation, but I'm not sure what I'm doing wrong there. I looked it up, and the violation ends in 00000000, which indicates that it's not able to find the file, which is why I wanted to ask - Do I need to put the entire filepath in the LoadFromFile('')?

  • If so - I tried to use tempdoc.LoadFromFile(tempbuffer+'shared.xml') - where Tempbuffer is the extracted filepath of everything up to that (in this case 'C:\users\...\Appdata\Roaming\Skype\')

  • I assume, like before, I'm using the part in the wrong way, but as I keep having to point out, this is all new to me. What am I doing wrong here? Is it because of the location of the file, or something else?

  • I haven't yet investigated it properly, because I'm still trying to work out how to actually read the .xml file, but I assume there's a way I can extract the child node from this:

 

<?xml version="1.0"?>
<config version="1.0" serial="56" timestamp="1412085296.3">
    <Lib>
    <Access>
        <Cookies>4100</Cookies>
    </Access>
    <Account>
        <Default>[ACCOUNTNAME]</Default>
    </Account>
    </Lib>
</config>

I'm trying to extract the [ACCOUNTNAME] field from the subnode. Hopefully this is clearer than the previous attempt.

Short version: How does LoadFromFile work? Can I extract a field from a node and turn it into a string?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Thomi
  • 17
  • 5
  • possible duplicate of [Libraries and tutorials for XML in Delphi](http://stackoverflow.com/questions/263419/libraries-and-tutorials-for-xml-in-delphi) Have a look at the `TXMLDocument` components as mentioned in that QA – Bernd Linde Oct 02 '14 at 12:08
  • Apologies, I did check that, but I found it difficult to understand how I was meant to get what I wanted out of the answer there, which is why I asked something more specific. – Thomi Oct 02 '14 at 12:11
  • You've not really asked something specific. You've asked for a broad tutorial. Anyone answering this is surely going to just duplicate the gazillion pre-existing tutorials. You've not even managed to show the XML and what you've tried. I think you need to try just a little harder. – David Heffernan Oct 02 '14 at 12:23
  • That's not XML. Please use real XML. And format it inside `
    ` tags
    – David Heffernan Oct 02 '14 at 13:10
  • void, fixed. How is not not XML? What makes XML into XML, etc? – Thomi Oct 02 '14 at 13:29
  • As it stands your snippet is not valid XML - it lacks closing tags for and . – 500 - Internal Server Error Oct 02 '14 at 13:40
  • Ah, yes, I see. It's just a snippet because I didn't want to paste the entire 150 line document into the question. – Thomi Oct 02 '14 at 13:43
  • Possible duplicate of http://stackoverflow.com/questions/9980164/how-do-i-access-the-child-node-values-in-my-xml-document – David Heffernan Oct 02 '14 at 14:00
  • It's impossible to answer questions that say "My code isn't working" when you don't include any code. A single in-line code snippet isn't "including your code", as there is no context, variable declarations and types, or any other useful information. If you want help determining why your code isn't working, *include the relevant code* in your question. – Ken White Oct 02 '14 at 15:42

1 Answers1

4

The reason why you are getting the Acces Violation on

tempdoc.LoadFromFile('document.xml');

is most likely the fact that TXMLDocument has not been created yet, such as by

tempdoc := TXMLDocument.Create(Form1);

Or dropping a TXMLDocument component onto the Form at design-time.

Then you can call

tempdoc.LoadFromFile('document.xml');

Or, if you want to load an existing file right away, by

tempdoc := TXMLDocument.Create('document.xml');

But then tempdoc1 **MUST** be declared asIXMLDocumentinstead ofTXMLDocument`.

The only time when you do not need to create TXMLDocument in code is if you have placed it onto the Form from Component Palette, then it gets automatically created with the Form.

BTW when an access violation occurs at address 00000000, it means you are trying to acces a nil pointer, such as an uninitialized object, or an already freed object.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Small correction: Address 00000000 does not imply "an already freed object". A reference can be set to nil without destroying an object, and conversely Freeing an object does not mean the reference was also set to nil. – Disillusioned Oct 03 '14 at 14:01