Getting Started with the XML Engine |
Assigning a Libref |
The XML engine works much like other SAS engines. That is, you execute a LIBNAME statement in order to assign a libref and specify an engine. You use that libref throughout the SAS session where a libref is valid.
A libref for the XML engine can be associated with either a specific XML document or, for the XML92 engine nickname, the physical location of a SAS library in a directory-based environment. When you use the libref, SAS either translates the data in a SAS data set into XML markup, or translates the XML markup into SAS format.
Importing an XML Document |
To import an XML document as a SAS data set, the following LIBNAME statement assigns a libref to a specific XML document and specifies the XML engine:
libname myxml xml 'C:\My Files\XML\Students.xml';
Executing the DATASETS procedure shows that SAS interprets the XML document as a SAS data set:
proc datasets library=myxml;
PROC DATASETS Output for MYXML Library
Directory Libref MYXML Engine XML Physical Name C:\My Files\XML\Students.xml XMLType GENERIC XMLMap NO XMLMAP IN EFFECT Member # Name Type 1 STUDENTS DATA
The PRINT procedure results in the following output:
proc print data=myxml.students; run;
PROC PRINT Output of SAS Data Set MYXML.STUDENTS
The SAS System Obs STATE CITY ADDRESS NAME ID 1 Texas Huntsville 1611 Glengreen Brad Martin 1755 2 Texas Houston 11900 Glenda Zac Harvell 1522
Exporting an XML Document |
To export an XML document from a SAS data set, the LIBNAME statement for the XML engine assigns a libref to the XML document to be created.
In the following code, the first LIBNAME statement assigns the libref MYFILES to the SAS library that contains the SAS data set Singers. The second LIBNAME statement assigns the libref MYXML to the physical location of the XML document that is to be exported from Myfiles.Singers:
libname myfiles 'C:\My Files\'; libname myxml xml 'C:\My Files\XML\Singers.xml';
Executing these statements creates the XML document named Singers.XML:
data myxml.Singers; set myfiles.Singers; run;
<?xml version="1.0" encoding="windows-1252" ?> <TABLE> <SINGERS> <FirstName> Tom </FirstName> <Age> 62 </Age> </SINGERS> <SINGERS> <FirstName> Willie </FirstName> <Age> 70 </Age> </SINGERS> <SINGERS> <FirstName> Randy </FirstName> <Age> 43 </Age> </SINGERS> </TABLE>
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.