XML [examples]

Display object as XML
PRINT_XML
open database southwind
select * from shippers into object shipobj
print_xml(shipobj)
1Speedy Express(503) 55
2United Package(503) 55
3Federal Shipping(503)

https://www.lianja.com/doc/index.php/PRINT_XML()


Specify the default format for XML files created by SELECT…SAVE AS XML
SET_XMLFORMAT
set xmlformat to ADO
SELECT * FROM example;
  SAVE AS XML example
 
// In Visual Basic the file can then be loaded like this:
// Set adoPrimaryRS = New Recordset
// adoPrimaryRS.Open "example.xml"
 
// Excel example
set xmlformat to excel
open database southwind
use customers
copy to mycustomers.xml type xml
Lianja.showDocument("mycustomers.xml")

https://www.lianja.com/doc/index.php/SET_XMLFORMAT


Create a dynamic array (object) from the contents of an XML formatted file
XML_DECODE_FILR
filename = geturl("http://www.myserver.com/getsomepage.rsp?name=smith&month=10", 30, array(), "myfilename.xml")
myobject = xml_decode_file(filename, "customers", "customer")

https://www.lianja.com/doc/index.php/XML_DECODE_FILE()


Replace fields in the current record with values from a character string containing XML formatted data
XML_GATHER
open database southwind
use shippers
cVar = xml_scatter()
copy structure to temp
use temp
append blank
xml_gather(cVar)

https://www.lianja.com/doc/index.php/XML_GATHER()


Return a character string containing XML decoded as an object
XML_DECODE
// Proccess the message queue
do while .t.
   // Check for new messages
   do while (mqcurmsgs(m_mqdes) > 0)
       // retrieve message
       m_message = mqreceive(m_mqdes)
           // decode the XML message into an object/dynamic array
           m_objmessage = xml_decode(m_message)
           // process the message
       mq_process_message(m_objmessage, m_message)
   enddo
   sleep 1
enddo

https://www.lianja.com/doc/index.php/XML_DECODE()


Encode an object as a character string containing XML for use with the messaging functions to pass objects between processes
XML_ENCODE
// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, xml_encode(tran_obj))
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
mqclose(mqdes)

https://www.lianja.com/doc/index.php/XML_ENCODE()


Encode an object as a character string containing XML for use with the messaging functions to pass objects between processes
XML_DECODE
// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, xml_encode(tran_obj))
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
mqclose(mqdes)

https://www.lianja.com/doc/index.php/XML_ENCODE()


Parse XML strings using XPath notation
XQUERY
// Examples using mybooks.xml
cMybooks = filetostr("mybooks.xml")
cBook = xquery(cMybooks,"/bookstore/book[2]")
? cBook
 
  Guy Watson
  Jane Baxter
  2008
  Paperback
 
cMybooks = filetostr("mybooks.xml")
cTitle = xquery(cMybooks,"/bookstore/book[2]/title")
? cTitle
 
Riverford Farm Cook Book

https://www.lianja.com/doc/index.php/XQUERY()


Search for an XML node in an XML file previously opened with XQUERY_OPEN() and return the complete node and its attributes
XQUERY_ATTRIBUTES
// Example using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_node("/bookstore/book[&i]/title")
   ? cTitle
   oTitle = xquery_attributes("/bookstore/book[&i]/title")
   ? xquery_decode(oTitle)
endfor
xquery_close()
 
// Results:
 
Pulse
 
Dynarray (refcnt=0)
(
    [title] => Dynarray (refcnt=1)
        (
            [text] => Pulse
            [attributes] => Dynarray (refcnt=1)
                (
                    [lang] => en
                )
        )
)
Riverford Farm Cook Book
 
Dynarray (refcnt=0)
(
    [title] => Dynarray (refcnt=1)
        (
            [text] => Riverford Farm Cook Book
            [attributes] => Dynarray (refcnt=1)
                (
                    [lang] => en
                )
        )
)
The House At Pooh Corner
 
Dynarray (refcnt=0)
(
    [title] => Dynarray (refcnt=1)
        (
            [text] => The House At Pooh Corner
            [attributes] => Dynarray (refcnt=1)
                (
                    [lang] => en
                )
        )
)
Knots and Crosses
 
Dynarray (refcnt=0)
(
    [title] => Dynarray (refcnt=1)
        (
            [text] => Knots and Crosses
            [attributes] => Dynarray (refcnt=1)
                (
                    [lang] => en
                )
        )
)
Le mythe de Sisyphe
 
Dynarray (refcnt=0)
(
    [title] => Dynarray (refcnt=1)
        (
            [text] => Le mythe de Sisyphe
            [attributes] => Dynarray (refcnt=1)
                (
                    [lang] => fr
                )
        )
)

https://www.lianja.com/doc/index.php/XQUERY_ATTRIBUTES()


Close an XML file previously opened with XQUERY_OPEN()
XQUERY_CLOSE
// Examples using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_find("/bookstore/book[&i]/title")
   ? cTitle
endfor
xquery_close()
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe
 
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
	nCountAuthor = xquery_count("/bookstore/book[&i]/author")
	for j = 1 to nCountAuthor
		cAuthor = xquery_find("/bookstore/book[&i]/author[&j]")
		? cAuthor
	endfor
endfor
xquery_close()
 
Jenny Chandler
Guy Watson
Jane Baxter
A. A. Milne
Ian Rankin
Albert Camus

https://www.lianja.com/doc/index.php/XQUERY_CLOSE()


Count the occurrences of an XML node in an XML file previously opened with XQUERY_OPEN()
XQUERY_COUNT
// Examples using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_node("/bookstore/book[&i]/title")
   ? cTitle
endfor
xquery_close()
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe
 
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
	nCountAuthor = xquery_count("/bookstore/book[&i]/author")
	for j = 1 to nCountAuthor
		cAuthor = xquery_node("/bookstore/book[&i]/author[&j]")
		? cAuthor
	endfor
endfor
xquery_close()
 
Jenny Chandler
Guy Watson
Jane Baxter
A. A. Milne
Ian Rankin
Albert Camus

https://www.lianja.com/doc/index.php/XQUERY_COUNT()


Return an object from an XML string
XQUERY_DECODE
// Example using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   oBook = xquery_node("/bookstore/book[&i]")
   ? xquery_decode(oBook)
endfor
xquery_close()
 
// Results:
Dynarray (refcnt=0)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Pulse
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Jenny Chandler
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 2013
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => COOKING
                        )
                )
        )
)
 
Dynarray (refcnt=0)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Riverford Farm Cook Book
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Jane Baxter
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 2008
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Paperback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => COOKING
                        )
                )
        )
)
 
Dynarray (refcnt=0)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => The House At Pooh Corner
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => A. A. Milne
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 1928
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => CHILDREN
                        )
                )
        )
)
 
Dynarray (refcnt=0)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Knots and Crosses
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Ian Rankin
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 2008
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => EPUB
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => CRIME
                        )
                )
        )
)
 
Dynarray (refcnt=0)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Le mythe de Sisyphe
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => fr
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Albert Camus
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 1943
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Paperback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => PHILOSOPHY
                        )
                )
        )
)

https://www.lianja.com/doc/index.php/XQUERY_DECODE()


Parse an XML file using XPath notation
XQUERY_FILE
// Examples using mybooks.xml
cBook = xquery_file("mybooks.xml","/bookstore/book[2]")
? cBook
 
  Guy Watson
  Jane Baxter
  2008
  Paperback
 
cTitle = xquery_file("mybooks.xml","/bookstore/book[2]/title")
? cTitle
 
Riverford Farm Cook Book
 
nCount = 1
do while .T.
    cTitle = xquery_file("mybooks.xml","/bookstore/book[&nCount]/title")
    if empty(cTitle)
        exit
	endif
    ? cTitle
    ++ nCount 
enddo
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe

https://www.lianja.com/doc/index.php/XQUERY_FILE()


Search for an XML node in an XML file previously opened with XQUERY_OPEN() and return its value
XQUERY_FIND
// Examples using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_find("/bookstore/book[&i]/title")
   ? cTitle
endfor
xquery_close()
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe
 
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
	nCountAuthor = xquery_count("/bookstore/book[&i]/author")
	for j = 1 to nCountAuthor
		cAuthor = xquery_find("/bookstore/book[&i]/author[&j]")
		? cAuthor
	endfor
endfor
xquery_close()
 
Jenny Chandler
Guy Watson
Jane Baxter
A. A. Milne
Ian Rankin
Albert Camus

https://www.lianja.com/doc/index.php/XQUERY_FIND()


Search for an XML node in an XML file previously opened with XQUERY_OPEN() and return its value
XQUERY_NODE
// Examples using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_node("/bookstore/book[&i]/title")
   ? cTitle
endfor
xquery_close()
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe
 
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
	nCountAuthor = xquery_count("/bookstore/book[&i]/author")
	for j = 1 to nCountAuthor
		cAuthor = xquery_node("/bookstore/book[&i]/author[&j]")
		? cAuthor
	endfor
endfor
xquery_close()
 
Jenny Chandler
Guy Watson
Jane Baxter
A. A. Milne
Ian Rankin
Albert Camus

https://www.lianja.com/doc/index.php/XQUERY_NODE()


Open XML files for parsing using XPath notation
XQUERY_OPEN
// Examples using mybooks.xml
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
   cTitle = xquery_node("/bookstore/book[&i]/title")
   ? cTitle
endfor
xquery_close()
 
Pulse
Riverford Farm Cook Book
The House At Pooh Corner
Knots and Crosses
Le mythe de Sisyphe
 
xquery_open("mybooks.xml")
nCount = xquery_count("/bookstore/book")
for i=1 to nCount
	nCountAuthor = xquery_count("/bookstore/book[&i]/author")
	for j = 1 to nCountAuthor
		cAuthor = xquery_node("/bookstore/book[&i]/author[&j]")
		? cAuthor
	endfor
endfor
xquery_close()
 
Jenny Chandler
Guy Watson
Jane Baxter
A. A. Milne
Ian Rankin
Albert Camus

https://www.lianja.com/doc/index.php/XQUERY_OPEN()


Query nodes by attribute and value in an XML file previously opened with XQUERY_OPEN()
XQUERY_SELECT
// Examples using mybooks.xml
xquery_open("mybooks.xml")
xquery_select("book[@category='children']")
obj = xquery_decode()
? obj
xquery_close()
 
// Results:
 
Dynarray (refcnt=1)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => The House At Pooh Corner
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => A. A. Milne
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 1928
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => CHILDREN
                        )
                )
        )
)
 
xquery_open("mybooks.xml")
xquery_select("/bookstore/book[year>'2010']")
obj = xquery_decode()
? obj
xquery_close()
 
// Results:
 
Dynarray (refcnt=1)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Pulse
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Jenny Chandler
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 2013
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => COOKING
                        )
                )
        )
)

https://www.lianja.com/doc/index.php/XQUERY_SELECT()


Return the contents of a record formatted as XML
XML_SCATTER
open database southwind
use shippers
cVar = xml_scatter()

https://www.lianja.com/doc/index.php/XML_SCATTER()


Number of records from an XML file
XMLCOUNT
select company, street, town, state, zip from sales save as xml sales
? xmlcount("sales.xml")
        25

https://www.lianja.com/doc/index.php/XMLCOUNT()


Create a Document Type definition file for a particular table
XMLCREATEDTD
use prospect
set xmlformat to recital
? xmlcreatedtd()
.T.

https://www.lianja.com/doc/index.php/XMLCREATEDTD()


Read the first record contained in the specified XML file and return the number of fields in the record
XMLFIRST
number = xmlfirst("sales.xml", target, trans, where, names, data)
? number
        30

https://www.lianja.com/doc/index.php/XMLFIRST()


Read the next record contained in the XML file specified with the XMLFIRST() function and return the number of fields in the record
XMLNEXT
number = xmlnext(trans, where, names, data)
? number
        30

https://www.lianja.com/doc/index.php/XMLNEXT()


Validate an XML file against its DTD file
XMLVALIDATE
? xmlvalidate("sales.xml")
.T.

https://www.lianja.com/doc/index.php/XMLVALIDATE()


create table customer2;
  from xml cust.xml load
insert into products;
  from xml prices.xml

Export

use example
copy to custxml xml
select employeeid, lastname, firstname;
  from employees into xml emp
select employeeid, lastname, firstname;
  from employees save as xml emp

https://www.lianja.com/doc/index.php/Importing_and_Exporting_Data


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.