tags:

views:

156

answers:

3

Hi all,

I am building an xml parser and constructor using the XDOM functions in UniVerse 10.1.0. Things are going fine until I get to use the XDOMAddChild function. I can add single elements fine using the handles but I get an error when adding a tree. The manuals indicate adding a tree is fine.

XDOMAddChild function

Syntax

XDOMAddChild(xmlHandle, xpathString, nsMap, nodeHandle, dupFlag)

Description

The XDOMAddChild function finds the xpathString in the context xmlHandle in the DOM structure and inserts a node nodeHandle as the last child of the found node. If the inserted node type is XDOM.ATTR.NODE, this node is inserted as an attribute.

Parameters

The following table describes each parameter of the syntax. Parameter Description

xmlHandle The handle to the context. [IN] xpathString Relative or absolute Xpath string. [IN] nsMap The map of namespaces which resolve the prefixes in the xpath string. Format is “xmlns=default_url xmlns:prefix1=prefix1_url xmlns:prefix2=prefix2_url”

For example: “xmlns=http://myproject.mycompany.com xmlns:a_prefix=a.mycompany.com” [IN]

nodeHandle Handle to a DOM subtree. If nodeHandle points to a DOM document, all of its children are inserted, in the same order. [IN]

dupFlag XDOM.DUP: Clones nodeHandle, and inserts the duplicate node. XDOM.NODUP: Inserts the original node. The subtree is also removed from its original location. [IN]

I accept the XDOM faults and flaws (particularly with building namespace prefixes) and an willing to work with them, But this one may be a show stopper with the current logic I am using. This is some test code:

$INCLUDE UNIVERSE.INCLUDE XML.H                                           
DATA.REQ = '<logonResponse></logonResponse>'
TEST.CHILD = '<logon>HELP</logon>'
NSMAP = ''
*
ERR$ = XDOMOpen(DATA.REQ, XML.FROM.STRING, DOM$H)                
  Status = XMLGetError(ERR$,errMsg)
  PRINT ERR$:',':errMsg 
ERR$ = XDOMOpen(TEST.CHILD, XML.FROM.STRING, CHILD$H)                
  Status = XMLGetError(ERR$,errMsg)
  PRINT ERR$:',':errMsg
ERR$ = XDOMLocate(DOM$H,'//logonResponse',NSMAP,NOD$H)                                   
  Status = XMLGetError(ERR$,errMsg)
  PRINT ERR$:',':errMsg
ERR$ = XDOMWrite(NOD$H,JUNK,XML.TO.STRING)
PRINT JUNK
ERR$ = XDOMWrite(CHILD$H,JUNK,XML.TO.STRING)
PRINT JUNK
ERR$ = XDOMAddChild(DOM$H, '',NSMAP,CHILD$H, XDOM.NODUP)
IF ERR$ = XML.SUCCESS THEN PRINT 'SUCCESS'
IF ERR$ = XML.ERROR THEN PRINT 'ERROR'
IF ERR$ = XML.INVALID.HANDLE THEN PRINT 'INVALID'
  Status = XMLGetError(ERR$,errMsg)
  PRINT ERR$:',':errMsg
END

I get this back....

1004,A DOM exception occured in function XDOMAddChild, DOM Exception code: 4

If I use XDOMCreateNode to create the TEST.CHILD element, the XDOMAddChild works fine. The only difference I can prove ids that the inserted node hanlde is different between teh 2 examples. The one that works the nodehandle is XDOM.ELEMENT.NODE (Type 1) and the one that fails in XDOM.DOC.NODE (Type 9).

Not sure where to go from here. Any code snippets, links, pointers; greatly appreciated

A: 

An XML Document is a different kind of beast than an XML Element. A document must contain one, and only one element: the document root, which contains the other elements. It's can't directly contain attribute or text nodes. It can contain DTDs, processing instructions, and a declaration, all of which are forbidden for an element to contain. An element can contain nodes; either text, attributes or other elements. An element can't contain a document.

You answered your own question here:

If I use XDOMCreateNode to create the TEST.CHILD element, the XDOMAddChild works fine. The only difference I can prove ids that the inserted node hanlde is different between teh 2 examples. The one that works the nodehandle is XDOM.ELEMENT.NODE (Type 1) and the one that fails in XDOM.DOC.NODE (Type 9).

Don't use XDOMOpen, use XDOMCreateNode OR XDOMLocate. You need an element, attribute or text node. A document won't work.

Fenugreek Femtosecond
A: 

Hi there. I actually made a small blog post regarding U2 XDom errors just before Christmas.

I assume UniVerse and UniData use the same XDOM parsers, so you can should be able to work out what that error refers to from the Xalan documentation.

In this case, Error 4 refers to 'WRONG_DOCUMENT_ERR'

Hope it helps somewhat...

Dan McGrath
A: 

Thanks,

The crux of my problem was that I was confused about the "Document" type as opposed to the "element" type.

eg: You can only use XDOMLocate on the "Document" Type Node. I was trying to use this function on a "element" Type node. The code below will no work because the 2nd Locate is performed on an "element" Type node.

  XMLDOC = <rootNode><nodeItem><data>aaa</data></nodeItem><nodeItem><data>bbb</data></nodeItem></rootNode>

  ERR$ = XDOMLocate(DOM$H,'//rootNode','',NOD$H)
  ERR$ = XDOMLocateNode(NOD$H,XDOM.CHILD,XDOM.LAST.CHILD,XDOM.ELEMENT.NODE,SUBNOD$H)
  ERR$ = XDOMLocate(SUBNOD$H,'//nodeItem/data','',DATA$H)
  ERR$ = XDOMLocateNode(DATA$H, XDOM.CHILD, XDOM.FIRST.CHILD, XDOM.TEXT.NODE, TEXT.NODE)
  ERR$ = XDOMGetNodeValue(TEXT.NODE, DATA$VALUE)

DATA$VALUE will not be extracted to "bbb". This is how to do it..

  ERR$ = XDOMLocate(DOM$H,'//rootNode/nodeItem[2]/data'','',DATA$H)
  ERR$ = XDOMLocateNode(DATA$H, XDOM.CHILD, XDOM.FIRST.CHILD, XDOM.TEXT.NODE, TEXT.NODE)
  ERR$ = XDOMGetNodeValue(TEXT.NODE, DATA$VALUE)

All nodes are not created equal.

jabber