David
Here is a simple example (UV syntax, adjust for UniData accordingly)
Hope this makes sense. I'll turn it into a knowledge base article when I
have time.
Assuming the following XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ROOT SYSTEM "titles.dtd">
<ROOT>
<BOOK_TITLES>
<TITLE_ID>10</TITLE_ID>
<SHORT_TITLE>Hancock a Comedy Genius (BBC Radio Collection)</SHORT_TITLE>
<BOOK_TITLES_AUTHOR_ID-MV>
<AUTHOR_ID>10</AUTHOR_ID>
</BOOK_TITLES_AUTHOR_ID-MV>
<BOOK_TITLES_AUTHOR_NAME-MV>
<AUTHOR_NAME>Cast</AUTHOR_NAME>
</BOOK_TITLES_AUTHOR_NAME-MV>
<ISBN>0563525452</ISBN>
</BOOK_TITLES>
(etc)
I want to populate a file named NEWTITLES, with the following dictionary:
DICT NEWTITLES 09:31:35am 04 Dec 2006 Page 1
Field......... Type & Field........ Conversion.. Column......... Output
Depth &
Name.......... Field. Definition... Code........ Heading........ Format
Assoc..
Number
ID D 0 Id 10L S
@ID D 0 NEWTITLES 10L S
TITLE D 1 Title 30L S
AUTHOR_ID D 2 Author_id 5L S
AUTHOR_NAME D 3 Author_name 30L S
ISBN D 4 Isbn 30L S
I create an extraction record in &XML& named newtitles.ext. This defines the
XPATH path to each dictionary element:
<U2XML_extraction xmlns:U2xml="http://www.ibm.com/U2-xml">
<file_extraction start="/ROOT" dictionary="NEWTITLES" null="EMPTY" />
<field_extraction field="@ID" path="BOOK_TITLES/TITLE_ID/text()" />
<field_extraction field="TITLE" path="BOOK_TITLES/SHORT_TITLE/text()" />
<field_extraction field="ISBN" path="BOOK_TITLES/ISBN/text()" />
</U2XML_extraction>
And then reference the map and the data in a simple program:
PROGRAM xml2
$INCLUDE UNIVERSE.INCLUDE XML.H
Ok = PrepareXML("&XML&\titles.xml",hXML)
If Ok = XML.ERROR Then
Ok = XMLError(text)
Crt "Error preparing document ":text
STOP
End
! First use a map to get the headers..
Ok = OpenXMLData(hXML, "&XML&\newtitles.ext",hXMLDATA)
If Ok = XML.ERROR Then
Crt "Error opening XML"
Ok = XMLError(text)
Crt text
STOP
End
Ok= ReadXMLData(hXMLDATA, Rec)
If Ok = XML.ERROR then
Crt "Error reading XML"
Ok = XMLError(text)
Crt text
End Else
Crt "Rec = ":Rec
End
Ok = CloseXMLData(hXMLDATA)
* Now the detail
Crt "Done"
OR do it the hard way using the XDOM API (required if you have, for example,
CDATA sections that are inadmissible in XPATH syntax):
PROGRAM xml3
* Using DOM
$INCLUDE UNIVERSE.INCLUDE XML.H
If XDOMOpen("&XML&\titles.xml",XML.FROM.FILE,hDOM) = XML.ERROR Then
Mess = "Cannot open DOM " ; GoSub ShowError
STOP
End
GoSub Parse
Ok = XDOMClose(hDOM)
STOP
Parse:
* First get my context (ROOT tag)
If XDOMLocate(hDOM,"/ROOT","",hParent) = XML.ERROR Then
Mess = "Cannot get parent elements"
GoSub ShowError
RETURN
End
* get the first child (should be BOOK_TITLES)
If XDOMLocateNode(hParent, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hNode)= XML.ERROR Then
Mess = "Cannot get main element"
goSub ShowError
RETURN
End
GoSub ShowNode
* get the eac remaining child (should be each order line)
LineNumber = 0
Loop
LineNumber += 1
While XDOMLocateNode(hParent, XDOM.CHILD, LineNumber + 1,
XDOM.ELEMENT.NODE, hNode)<> XML.ERROR Do
Crt "Got title : ":LineNumber
GoSub ShowNode
Repeat
Return
ShowError:
Crt "Error : ":Mess
Ok = XMLGetError(Code, ErrText)
Crt "Code = ":Code
Crt "Text = ":ErrText
Return
*---------------------------------------------------------------------------
* ShowNode
*---------------------------------------------------------------------------
ShowNode:
Ok = XDOMGetNodeName(hNode,NodeName)
Crt "Node Name = ":NodeName
* This should be the title id
! Getting the first element
If XDOMLocateNode(hNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hValueNode) <> XML.ERROR Then
GoSub ShowValueNode
End
* And this should be the title name
If XDOMLocateNode(hNode, XDOM.CHILD, 2, XDOM.ELEMENT.NODE, hValueNode)
<> XML.ERROR Then
GoSub ShowValueNode
End
Return
*---------------------------------------------------------------------------
* ShowValueNode
*---------------------------------------------------------------------------
ShowValueNode:
Ok = XDOMGetNodeName(hValueNode,NodeName)
Crt "Node Name = ":NodeName
If XDOMLocateNode(hValueNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.TEXT.NODE, hTextNode) <> XML.ERROR THen
ok = XDOMGetNodeValue(hTextNode, Value)
Crt "Value = ":Value
end
Return
Hope this helps,
Regards,
Brian
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of
> David Wolverton
> Sent: 04 December 2006 03:29
> To: [email protected]
> Subject: [U2] Creating XML Extraction File
>
> I feel like I'm missing something -- it appears I need to
> build a file that ends in "EXT" to use with the OpenXML
> command in UniBASIC.
>
> Yet, what that file contains according to the (poor)
> documentation 'feels'
> like what I just spent an hour doing in the XML/DB Tool - but
> I didn't get an EXT file out of the deal - and I can't find a
> way to get one without opening (fill in your favorite text
> editor here).
>
> I looked at Clif's slide presentation referenced in other
> emails, but it doesn't reference the EXT file design.
>
> Am I doing something wrong, or does the Mapping file creation
> still need the EXT file to be built manually? It would seem
> the DBTool would handle this, so I'm hoping I'm missing something!
>
> Thanks for any thoughts...
>
> David Wolverton
> -------
> u2-users mailing list
> [email protected]
> To unsubscribe please visit http://listserver.u2ug.org/
-------
u2-users mailing list
[email protected]
To unsubscribe please visit http://listserver.u2ug.org/