Importing XML into SPSS

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Importing XML into SPSS

Luca Meyer-3
Hi,

A customer is proposing to send me data in XML sort of:

===

<?xml version="1.0" encoding="utf-8" ?>
<data activity="blitz">
        <domanda>
                <testoDomanda>Do you know the following brands?</testoDomanda>
                <risposte>
                        <risposta>BRAND_A</risposta>
                        <risposta>BRAND_B</risposta>
                </risposte>
        </domanda>
        <domanda>
                <testoDomanda>Do you agree with the following statements about BRAND_A?</testoDomanda>
                <risposte>
                        <risposta>It is highly priced</risposta>
                        <risposta>It has high value</risposta>
                        <risposta>Other</risposta>
                </risposte>
        </domanda>
        <!-- Altre domande... -->
</data>
===
Please notice that domanda=question, risposte=answers, altre domande=other questions.
Can I import this data into a SPSS? If so, how can I do that?

Thanks,
Luca


Luca Meyer
www.lucameyer.com
PASW Statistics v. 18.0.1 (13-nov-2009)
Mac OS X 10.6.2 (10C540) - kernel Darwin 10.2.0





Reply | Threaded
Open this post in threaded view
|

Re: Importing XML into SPSS

Garry Gelade

You could write a Python routine.

 

Garry

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Luca Meyer
Sent: 25 March 2010 13:05
To: [hidden email]
Subject: Importing XML into SPSS

 

Hi,

 

A customer is proposing to send me data in XML sort of:

 

===

 

<?xml version="1.0" encoding="utf-8" ?>

<data activity="blitz">
        <domanda>
                <testoDomanda>Do you know the following brands?</testoDomanda>
                <risposte>
                        <risposta>BRAND_A</risposta>
                        <risposta>BRAND_B</risposta>
                </risposte>
        </domanda>
        <domanda>
                <testoDomanda>Do you agree with the following statements about BRAND_A?</testoDomanda>
                <risposte>
                        <risposta>It is highly priced</risposta>
                        <risposta>It has high value</risposta>
                        <risposta>Other</risposta>
                </risposte>
        </domanda>
        <!-- Altre domande... -->
</data>
===
Please notice that domanda=question, risposte=answers, altre domande=other questions.

Can I import this data into a SPSS? If so, how can I do that?

 

Thanks,

Luca

 

 

Luca Meyer
www.lucameyer.com
PASW Statistics v. 18.0.1 (13-nov-2009)
Mac OS X 10.6.2 (10C540) - kernel Darwin 10.2.0








__________ Information from ESET NOD32 Antivirus, version of virus signature database 4973 (20100325) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4974 (20100325) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Reply | Threaded
Open this post in threaded view
|

Re: Importing XML into SPSS

Jon K Peck
In reply to this post by Luca Meyer-3



From: Jon K Peck/Chicago/IBM
To: Luca Meyer <[hidden email]>
Cc: [hidden email]
Date: 03/25/2010 12:33 PM
Subject: Re: [SPSSX-L] Importing XML into SPSS




You may need some custom programming in Python or XSLT or some other xml-aware tool to massage the data, but the particular xml you posted reads beautifully into Excel 2007 as a data table.

I can't attach the actual result as the dragon list servers consumes it in flames, but here is the text
activity        testoDomanda                                                                                risposta
blitz        Do you know the following brands?                                            BRAND_A
blitz        Do you know the following brands?                                            BRAND_B
blitz        Do you agree with the following statements about BRAND_A?        It is highly priced
blitz        Do you agree with the following statements about BRAND_A?        It has high value
blitz        Do you agree with the following statements about BRAND_A?        Other

Regards,
Jon Peck

p.s. The SPSS Data Access Pack used to include an xml driver, but it didn't work all that well, and I don't know whether some version of it is still included.



From: Luca Meyer <[hidden email]>
To: [hidden email]
Date: 03/25/2010 07:11 AM
Subject: [SPSSX-L] Importing XML into SPSS
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi,

A customer is proposing to send me data in XML sort of:

===

<?xml version="1.0" encoding="utf-8" ?>
<data activity="blitz">
       <domanda>
               <testoDomanda>Do you know the following brands?</testoDomanda>
               <risposte>
                       <risposta>BRAND_A</risposta>
                       <risposta>BRAND_B</risposta>
               </risposte>
       </domanda>
       <domanda>
               <testoDomanda>Do you agree with the following statements about BRAND_A?</testoDomanda>
               <risposte>
                       <risposta>It is highly priced</risposta>
                       <risposta>It has high value</risposta>
                       <risposta>Other</risposta>
               </risposte>
       </domanda>
       <!-- Altre domande... -->
</data>

===
Please notice that domanda=question, risposte=answers, altre domande=other questions.
Can I import this data into a SPSS? If so, how can I do that?

Thanks,
Luca


Luca Meyer
www.lucameyer.com
PASW Statistics v. 18.0.1 (13-nov-2009)
Mac OS X 10.6.2 (10C540) - kernel Darwin 10.2.0







Reply | Threaded
Open this post in threaded view
|

Re: Importing XML into SPSS

Albert-Jan Roskam
In reply to this post by Luca Meyer-3
Hi Luca,

The following code produces one line per question. Column 1 contains the question, Col 2:n contain the answers. The layout that Jon Peck gave may be more desirable, but I didn't figure out how to do that yet.

import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="utf-8" ?>

<data activity="blitz">
        <domanda>
                <testoDomanda>Do you know the following brands?</testoDomanda>
                <risposte>
                        <risposta>BRAND_A</risposta>
                        <risposta>BRAND_B</risposta>
                </risposte>
        </domanda>
        <domanda>
                <testoDomanda>Do you agree with the following statements about BRAND_A?</testoDomanda>
                <risposte>
                        <risposta>It is highly priced</risposta>
                        <risposta>It has high value</risposta>
                        <risposta>Other</risposta>
                </risposte>
        </domanda>
        <!-- Altre domande... -->
</data>"""

f = open("d:/temp/xml2tab.txt", "wb")
tree = ET.fromstring(data) # or: ET.parse(xml_filename)
firstline = True
for p in tree.getiterator():
    print "%s\t%s" % (p.tag, p.text)
    if p.tag == "testoDomanda":
        if firstline:
            firstline = False
            f.write("%s\t" % (p.text))           
        else:
            f.write("\r\n%s\t" % (p.text))
    elif p.tag == "risposta":
        f.write("%s\t" % (p.text))
f.close()

GET DATA
  /TYPE=TXT
  /FILE='D:\temp\xml2tab.txt'
  /DELCASE=LINE
  /DELIMITERS="\t"
  /ARRANGEMENT=DELIMITED
  /FIRSTCASE=1
  /IMPORTCASE=ALL
  /VARIABLES=
  q A60
  a1 A20
  a2 A20
  a3 A20.
CACHE.
EXECUTE.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 3/25/10, Luca Meyer <[hidden email]> wrote:

From: Luca Meyer <[hidden email]>
Subject: [SPSSX-L] Importing XML into SPSS
To: [hidden email]
Date: Thursday, March 25, 2010, 2:04 PM

Hi,

A customer is proposing to send me data in XML sort of:

===

<?xml version="1.0" encoding="utf-8" ?>
<data activity="blitz">
<domanda>
<testoDomanda>Do you know the following brands?</testoDomanda>
<risposte>
<risposta>BRAND_A</risposta>
<risposta>BRAND_B</risposta>
</risposte>
</domanda>
<domanda>
<testoDomanda>Do you agree with the following statements about BRAND_A?</testoDomanda>
<risposte>
<risposta>It is highly priced</risposta>
<risposta>It has high value</risposta>
<risposta>Other</risposta>
</risposte>
</domanda>
<!-- Altre domande... -->
</data>
===
Please notice that domanda=question, risposte=answers, altre domande=other questions.
Can I import this data into a SPSS? If so, how can I do that?

Thanks,
Luca


Luca Meyer
www.lucameyer.com
PASW Statistics v. 18.0.1 (13-nov-2009)
Mac OS X 10.6.2 (10C540) - kernel Darwin 10.2.0