Trying to add a node to an existing XML file

Just starting out? Need help? Post your questions and find answers here.
Steving
User
User
Posts: 13
Joined: Tue Sep 12, 2017 11:40 pm
Location: San Francisco Bay Area
Contact:

Trying to add a node to an existing XML file

Post by Steving »

Hello all,

I'm working with creating an XML file and updating it with new data when I get new entries from a file. The code works fine then I create the XML file with CreateXML but when I load the file with LoadXML and try to add a node it gives me an error. There must be something that I'm missing.

Code: Select all

.
.
.
CheckFile = FileSize(MasterFileXMLFileName)
If CheckFile = -1
  LogSomething("Creating XML file")
  MasterXML = CreateXML(#PB_Any)
  LogSomething("MasterXML = " + MasterXML)
  MainNode = CreateXMLNode(RootXMLNode(MasterXML), "Customers")
  LogSomething("MainNode = " + MainNode)
Else
  LogSomething("Opening current XML file.")
  MasterXML = LoadXML(#PB_Any, MasterFileXMLFileName)
  StatusCheck = XMLStatus(MasterXML)
  If StatusCheck <> 0
    LogSomething("Error loading the XML file. XML Status = " + StatusCheck + ". Error at " + XMLErrorLine(MasterXML) + " And Character " + XMLErrorPosition(MasterXML) + ": " + XMLError(MasterXML))
  Else
    LogSomething("XML File " + MasterFileXMLFileName + " loaded without error.")
  EndIf
  LogSomething("MasterXML = " + MasterXML)
  MainNode = RootXMLNode(MasterXML)
  LogSomething("Main Node = " + MainNode)
EndIf

ForEach MasterFile()
  PatientNode = XMLNodeFromID(MasterXML, MasterFile()\PSID)
  If PatientNode = 0
    LogSomething("Adding " + MasterFile()\LastName + ", " + MasterFile()\FirstName + " (" + MasterFile()\PSID + ") to the system.")
    ; Create the XML node for this person
    CustomerDisplayName.s = RemoveString(RemoveString(RemoveString(Proper(MasterFile()\FirstName) + Proper(MasterFile()\LastName), " "),","),".")
    Customer = CreateXMLNode(MainNode, CustomerDisplayName)
    If Customer = 0
      LogSomething("Error creating XML node: " + Customer)
    EndIf
    SetXMLAttribute(Customer, "id", MasterFile()\PSID)
    Item = CreateXMLNode(Customer, "LastName")
    SetXMLNodeText(Item, Proper(MasterFile()\LastName))
    If Not (MasterFile()\LastName = #Empty$ Or MasterFile()\LastName = #Null$)
.
.
.
I am using option Explicit and when I delete the file and allow the program to recreate the file after it adds all the nodes it saves it

Code: Select all

.
.
.

FormatXML(MasterXML, #PB_XML_ReFormat | #PB_XML_ReIndent, 2)

SaveXML(MasterXML, MasterFileXMLFileName)
.
.
.
The whole thing works fine when I create a new file but when I load the file the first thing that happens when I try to add a new node is it comes back with a 0 on the CreateXMLNode line for "Customer" and errors out.

Any help would be much appreciated.

Sincerely,

Steving
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Trying to add a node to an existing XML file

Post by STARGÅTE »

In your small code example I can see "MainNode = CreateXMLNode(RootXMLNode(MasterXML), "Customers")" in case if you create the XML file, but this value is set to "MainNode = RootXMLNode(MasterXML)" in case of loading the file.
Shouldn't it be "MainNode = MainXMLNode(MasterXML)" (Customers)?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Steving
User
User
Posts: 13
Joined: Tue Sep 12, 2017 11:40 pm
Location: San Francisco Bay Area
Contact:

Re: Trying to add a node to an existing XML file

Post by Steving »

:D :D :D :D :D :D :D :D :D :D :D :D

I'm a happy dude over here! Your answer pointed me in the correct direction. The thing that fixed it was (for anyone else that's starting out with this)

Code: Select all

MainNode = XMLNodeFromPath(MainXMLNode(MasterXML), "/Customers")
Once I added that it worked and I was able to add additional peeps to my file and even save it! Thanks so much.

Just as a side note, I love this language and it's good to see other people out there still using it and have a place to get answers from when I get stuck. Thanks so much again!

Sincerely,

Steving
Post Reply