XML update issue

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AlexAdams
Posts: 38
Joined: Sat Jan 17, 2009 8:49 pm
Location: Santa Cruz, CA
Contact:

XML update issue

Post by AlexAdams » Sun Jul 26, 2009 6:10 pm

I'm building a standalone app that uses local XML files as data storage devices. I'm using the XML stack tutorial as my guide in constructing the methods.

Loading XML files is working fine. Add nodes is working fine. Saving files is working fine.

Updating nodes is not working.

Here are the contents of a test XML file.

<?xml version="1.0"?>
<contactTable>
<contact contactID="adamsalex">
<lastName>Adams</lastName>
<firstName>Alex</firstName>
<middleName/>
<companyName>A2 Technology Partners, Inc.</companyName>
<title>President</title>
<profession>Software Innovator</profession>
</contact>
<contact contactID="adamsnancykrogseng">
<lastName>Adams</lastName>
<firstName>Nancy</firstName>
<middleName>Krogseng</middleName>
<companyName>Nancy-Krogseng Adams PsyD.</companyName>
<title>Licensed Psychologist</title>
<profession>Psychologist</profession>
</contact>
</contactTable>

Here is the update handler code:

on updateRecord
global gvContactID
local lvStart, lvDocID, lvNode, lvChild
local lvContactID, lvLastName, lvFirstName, lvMiddleName, lvCompanyName, lvTitle, lvProfession

-- find the document ID - this routine creates one if there isn't one already
put field "fldDocID" of card 1 of this stack into lvDocID

-- build the full node address for the selected contact record
put "/contactTable/contact[" & gvContactID & "]" into lvNode

put field "fldLastName" of card "crdContactRecord" into lvLastName
put field "fldFirstName" of card "crdContactRecord" into lvFirstName
put field "fldMiddleName" of card "crdContactRecord" into lvMiddleName
put field "fldCompanyName" of card "crdContactRecord" into lvCompanyName
put field "fldTitle" of card "crdContactRecord" into lvTitle
put field "fldProfession" of card "crdContactRecord" into lvProfession
revPutIntoXMLNode lvDocID, lvNode & "/lastName", lvLastName
checkForError the result, " lastname ", lvNode
revPutIntoXMLNode lvDocID, lvNode & "/firstName", lvFirstName
checkForError the result, " firstname ", lvNode
revPutIntoXMLNode lvDocID, lvNode & "/middleName", lvMiddleName
checkForError the result, " middlename ", lvNode
revPutIntoXMLNode lvDocID, lvNode & "/companyName", lvCompanyName
checkForError the result, "c ompanyname ", lvNode
revPutIntoXMLNode lvDocID, lvNode & "/title", lvTitle
checkForError the result, " title ", lvNode
revPutIntoXMLNode lvDocID, lvNode & "/profession", lvProfession
checkForError the result, " profession ", lvNode

-- setup the display field for text instead of an XML tree
set the tabStops of field "fldContactTree" of card 1 of this stack to 60
put formatXMLtext(lvDocID) into field "fldContactTree" of card 1 of this stack
end updateRecord

Here is the checkForError code:

on checkForError pError, pField, pNode
if word 1 of pError contains "xmlErr" then
answer error pError & pField & pNode as sheet
exit to top
end if
end checkForError

Every time, I get the following result:

xmlerr, can't find element lastname
/contactTable/contact[adamsalex]

What am I doing wrong?

Thanks,

Alex Adams
A2 Technology Partners, Inc.
alex@a2technology.com
Alex Adams
(a)2 Technology Parnters
alex@a2technology.com
www.a2technology.com
www.promisstudio.com
831-726-8013

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Sun Jul 26, 2009 9:13 pm

The revPutIntoXmlNode command allows you to update an already existing node. You can add a new xml node and put data into it in one go, using the revAddXmlNode command. Also, you cannot expect the xml library to find your node on the basis of square brackets, translating it to some attribute to find the corresponding ndoe - the square brackets are used as sequential indicators, in case there's more than one sibling node with the same element name.

So you're looking at something like this for adding a new contact:

Code: Select all

-- find the document ID - this routine creates one if there isn't one already 
put field "fldDocID" of card 1 of this stack into lvDocID 

-- create the contact node
put "/contactTable" into lvParentNode
revAddXmlNode lvDocId, lvParentNode, "contact"

-- the new node was added at the end, so get a reference to it
put revXmlNumberOfChildren(lvDocId, lvParentNode) into tChildCount
put revXmlFirstChild(lvDocId, lvParentNode) into lvNode
repeat tChildCount - 1 times
  put revXmlNextSibling(lvDocId, lvNode) into lvNode
end repeat

-- now first set the contactId attribute
revSetXmlAttribute lvDocId, lvNode, "contactID", gvContactId

-- now add all the subnodes
put field "fldLastName" of card "crdContactRecord" into lvLastName 
revAssXMLNode lvDocID, lvNode & "/lastName", lvLastName 
checkForError the result, " lastname ", lvNode
If you want to update an existing node, you're going to have to find the right contact node. That's where the revXmlMathingNode function comes into play.

Code: Select all

- find the document ID - this routine creates one if there isn't one already 
put field "fldDocID" of card 1 of this stack into lvDocID 

-- find the contact node
put "/contactTable" into lvParentNode
put revXmlMatchingNode(lvDocId, lvParentNode, "contact", "contactID", gvContactId, 1) into lvNode

-- now update the child nodes
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply

Return to “Databases”