Help Reading XML files in Python: Urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Reading XML files in Python: Urgent
# 1  
Old 05-12-2009
Help Reading XML files in Python: Urgent

OK so my objective is to create a python program that will parse an XML file(input.xml), then the program will create an mxml(output.mxml) file.

In the program (.py) i need to read between CDATA so that I would get an output the CDATA code in the input.xml



INPUT.XML
<![CDATA[
<pre>
x = 5
if (x > 4):
print "x is bigger than 4"
</pre>
]]>
</htmlText>
----------------------------
program.py
self.outfile.write(' <mx:htmlText>\n')
self.outfile.write(' <![CDATA[\n')
self.outfile.write(HOW DO I READ BETWEEN CDATA OF XML FILE)
self.outfile.write('\n')
self.outfile.write(' ]]>\n')
self.outfile.write(' </mx:htmlText>\n')
----------------------------
OUTPUT MUST BE LIKE THIS
<![CDATA[
<pre>
x = 5
if (x > 4):
print "x is bigger than 4"
</pre>
]]>
</htmlText>

I'm sorry if I dont make any sense!
# 2  
Old 05-12-2009
if you are doing full fledge XML processing, use an XML processing module, such as PyXML (search google). otherwise, here's something simplistic
Code:
f=0
for line in open("file"):
    line=line.strip()
    if "</htmlText>" in line: print line;f=0
    if "<![CDATA[" in line: f=1
    if f:print line

# 3  
Old 05-12-2009
actually
Here is my program
Code:
import xml.sax
true = 1
false = 0

class MyHandler(xml.sax.ContentHandler):
    """ class to parse xml files to create mxml files """
    def __init__(self):
        xml.sax.ContentHandler.__init__(self)
        self.outfile = None
        self.inPage = false
        self.inQuestion = false
        self.inReply = false
        self.inAnswer = false
        self.inQuestionText = false
        self.inPageTitle = false
        self.inText = false
        self.inHtmlText = false
        self.replies = []
        self.answers = []
        self.storedReplies = []
        self.questionNumber = 1
        self.questionText = ""
        self.pageTitle= []
        self.text= ""
        self.htmlText= []
    
    def startElement(self,name,attr):
        if (name == "viewStack"):
            infile = open("header.in","r")
            lines = infile.readlines()
            infile.close()
            outputfile = raw_input("Enter output filename: ")
            self.outfile = open(outputfile,"w")
            for i in range(0, len(lines)):
                self.outfile.write(lines[i])
        elif (name == "page"):
            self.inPage = true
        elif (name == "question"):
            self.inQuestion = true
        elif (name == "reply"):
            self.inReply = true
        elif (name == "answer"):
            self.inAnswer = true
        elif (name == "questionText"):
            self.inQuestionText = true
        elif (name == "pageTitle"):
            self.inPageTitle = true
        elif (name == "text"):
            self.inText = true
        elif (name == "htmlText"):
            self.inHtmlText = true
                        
    def endElement(self,name):
        if (name == "viewStack"):
            self.outfile.write('            <mx:Panel title="End of Tutorial"\n')
            self.outfile.write('                height="100%" width="100%">\n')
            self.outfile.write('                <mx:Label width="100%"\n')
            self.outfile.write('                    text="All done" />\n')
            self.outfile.write('            </mx:Panel>\n')
            self.outfile.write('        </mx:ViewStack>\n')
            self.outfile.write('    </comp:ResizablePanel>\n')
            self.outfile.write('    <mx:Script>\n')
            self.outfile.write('        <![CDATA[\n')
            for i in range(0, len(self.storedReplies)):
                self.outfile.write('            private function ')
                self.outfile.write('handleq' + str(i+1))
                self.outfile.write('(Event:MouseEvent):void\n')
                self.outfile.write('            {\n') 
                for j in range(0, len(self.storedReplies[i])):
                    if (j == 0):
                        self.outfile.write('                if ')
                    else:
                        self.outfile.write('                else if ')
                    self.outfile.write('(q' + str(i+1))
                    self.outfile.write('_' + str(j+1))
                    self.outfile.write('.selected) {\n')
                    self.outfile.write('                    Alert.show("')
                    self.outfile.write(self.storedReplies[i][j])
                    self.outfile.write('","Result")\n')
                    self.outfile.write('                }\n')
                self.outfile.write('            }\n')
            self.outfile.write('        ]]>\n')
            self.outfile.write('    </mx:Script>\n')
            self.outfile.write('</mx:Application>\n')
            self.outfile.close()
        elif (name == "page"):
            self.inPage = false
        elif (name == "question"):
            self.inQuestion = false
            self.storedReplies.append(self.replies)
            self.replies = []
            self.outfile.write('                ')
            self.outfile.write('<mx:Text width="100%" ')
            self.outfile.write('color="blue" selectable="false"\n')
            self.outfile.write('                    ')
            self.outfile.write('text="' + self.questionText)
            self.outfile.write('" />\n')
            for i in range(0, len(self.answers)):
                self.outfile.write('                ')
                self.outfile.write('<comp:MultilineRadioButton>')
                self.outfile.write(' width="370" id="q')
                self.outfile.write(str(self.questionNumber))
                self.outfile.write('_' + str(i+1) + '" ')
                self.outfile.write('label="' + self.answers[i])
                self.outfile.write('" />\n')
            self.answers = []
            self.outfile.write('                ')
            self.outfile.write('<mx:Button label="Check Answer"\n')
            self.outfile.write('                    ')
            self.outfile.write('click="handleq')
            self.outfile.write(str(self.questionNumber))
            self.outfile.write('(event)" />\n')
            self.questionNumber = self.questionNumber + 1
        elif (name == "reply"):
            self.inReply = false
        elif (name == "answer"):
            self.inAnswer = false
        elif (name == "questionText"):
            self.inQuestionText = false
        elif (name == "pageTitle"):
            self.inPageTitle = false
            self.outfile.write('            ')
            self.outfile.write('<mx:Panel title="' + self.pageTitle)
            self.outfile.write(' "')
            self.outfile.write('\n')
            self.outfile.write('                ')
            self.outfile.write('width="100%" height="100%">\n')
        elif (name == "text"):
            self.inText = false
            self.outfile.write('                ')
            self.outfile.write('<mx:Text width="100%" selectable="false"\n')
            self.outfile.write('                    ')
            self.outfile.write('text="' + self.text)
            self.outfile.write('\n')
            self.outfile.write('                          ')
            self.outfile.write('" />\n')
        elif (name == "htmlText"):
            self.inHtmlText = false
            self.outfile.write('                  <mx:htmlText>\n')
            self.outfile.write('                    <![CDATA[\n')
            self.outfile.write('text="' + self.htmlText)
            self.outfile.write('" />\n')
            self.outfile.write('\n')
            self.outfile.write('                    ]]>\n')
            self.outfile.write('                  </mx:htmlText>\n')
            self.outfile.write('                </mx:Text>\n')
            self.outfile.write('            </mx:Panel>\n')

    def characters(self,data):
        if (self.inPage):
            if (self.inQuestion):
                if (self.inReply):
                    if (data.strip() != ""):
                        #print data.strip()
                        self.replies.append(data.strip())
                elif (self.inAnswer):
                    if (data.strip() != ""):
                        self.answers.append(data.strip())
                        print data.strip()
                elif (self.inQuestionText):
                    if (data.strip() != ""):
                        self.questionText = data.strip()
            elif (self.inPageTitle):
                if (data.strip() != ""):
                    self.pageTitle = data.strip()
            elif (self.inText):
                if (data.strip() != ""):
                    self.text = data.strip()
            elif (self.inHtmlText):
                if (data.strip() != ""):
                    self.htmlText = data.strip()


parser = xml.sax.make_parser()
handler = MyHandler()
parser.setContentHandler(handler)
inputfile = raw_input("Enter xml filename: ")
parser.parse(inputfile)

And My Current Output
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:MyComp="mypanel.test.*" xmlns:comp="*"
    backgroundColor="#ccffcc"
    backgroundAlpha="0"
    horizontalAlign="left"
    verticalGap="10" horizontalGap="10">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert
            private function nextClick(Event:MouseEvent):void
            {
                if (vs.selectedIndex < vs.numChildren - 1) {
                    vs.selectedIndex++;
                }
            }
            private function prevClick(Event:MouseEvent):void
            {
                if (vs.selectedIndex > 0) {
                    vs.selectedIndex--;
                }
            }
        ]]>
    </mx:Script>
    <comp:ResizablePanel width="470" height="340">
        <mx:HBox>
            <mx:Button label="Home" click="vs.selectedIndex=0" />
            <mx:Button label="Previous" click="prevClick(event)" />
            <mx:Button label="Next" click="nextClick(event)" />
        </mx:HBox>
        <mx:ViewStack id="vs"
            x="30" y="32"
            width="440" height="639"
            selectedIndex="0">
            <mx:Panel title="Welcome"
                height="100%" width="100%">
                <mx:Label width="100%"
                    text="When you are ready to proceed, hit Next" />
            </mx:Panel>            
            <mx:Panel title="page 1 "
                width="100%" height="100%">
                <mx:Text width="100%" selectable="false"
                    text="Paragraph 1 on page 1
                          " />
                  <mx:htmlText>
                    <![CDATA[
text="</pre>" />

                    ]]>
                  </mx:htmlText>
                </mx:Text>
            </mx:Panel>
            <mx:Panel title="Question page "
                width="100%" height="100%">
                <mx:Text width="100%" color="blue" selectable="false"
                    text="What is 3 + 5?" />
                <comp:MultilineRadioButton> width="370" id="q1_1" label="7" />
                <comp:MultilineRadioButton> width="370" id="q1_2" label="8" />
                <mx:Button label="Check Answer"
                    click="handleq1(event)" />
            <mx:Panel title="End of Tutorial"
                height="100%" width="100%">
                <mx:Label width="100%"
                    text="All done" />
            </mx:Panel>
        </mx:ViewStack>
    </comp:ResizablePanel>
    <mx:Script>
        <![CDATA[
            private function handleq1(Event:MouseEvent):void
            {
                if (q1_1.selected) {
                    Alert.show("Incorrect","Result")
                }
                else if (q1_2.selected) {
                    Alert.show("Yes, that is correct.","Result")
                }
            }
        ]]>
    </mx:Script>
</mx:Application>

Im trying to read in between the input file
Code:
<?xml version="1.0" ?>
<viewStack>
  <page>
    <pageTitle>
      page 1
    </pageTitle>
    <text>
      Paragraph 1 on page 1
    </text>
    <htmlText>
      <![CDATA[
      <pre>
x = 5
if (x > 4):
    print "x is bigger than 4"
</pre>
      ]]>
    </htmlText>
  </page>
  <page>
    <pageTitle>
      Question page
    </pageTitle>
    <question>
      <questionText>
        What is 3 + 5?
      </questionText>
      <questionNumber>
        q1
      </questionNumber>
      <answer>
        7
      </answer>
      <reply>
        Incorrect
      </reply>
      <answer>
        8
      </answer>
      <reply>
        Yes, that is correct.
      </reply>
    </question>
  </page>
</viewStack>

How do I read between the CDATA? this part I am having trouble to write through the program. CHECK IN BOLD
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

2. Programming

Decimals reading Python

Hello, i'm new in python. Consider that i have this function that read me some data from a serial : def GetData(): line = open(serialx).read() hash = line.find("#") when = line count = line # print when, count, line return (float(when), int(count)) it gives me the result... (2 Replies)
Discussion started by: Board27
2 Replies

3. AIX

Python on AIX-Need urgent Help

Hi Experts, I am new to python and AIX. I am trying to install Python 2.7 or python 3.2 on AIX 7.1 but getting the bellow error. could you please let me know how to resolve this. Note: Same has been installed on Linux Redhat successfully. $ ./configure --enable-shared ##... (1 Reply)
Discussion started by: Tamil_Arasan
1 Replies

4. Programming

Python Reading Individual files and Regex through them

As a newbie to Python, I am trying to write a script in which is will add all the log files (*.log) from within a directory to a list, open the files and search for an ip using a regex and single it out (appending the ip's to the list). So far, I have: import re, os def list_files() content = ... (4 Replies)
Discussion started by: metallica1973
4 Replies

5. Programming

Python reading from a file

Hello everyone, I've been learning some python (I was using other commercial software before), and doing plots from data stored on files as X and Y pairs has not been an issue. Know, I have some files that look like this: <Descriptive string> <some "random" number> <number of X values:nx>... (0 Replies)
Discussion started by: jaldo0805
0 Replies

6. Programming

Python reading from a file

Hello everyone, I've been learning some python (I was using other commercial software before), and doing plots from data stored on files as X and Y pairs has not been an issue. Know, I have some files that look like this: <Descriptive string> <some "random" number> <number of X values:nx>... (0 Replies)
Discussion started by: jaldo0805
0 Replies

7. UNIX for Dummies Questions & Answers

Urgent - XML Attribute Remove

Hi I have got a XML file which has got content as follows: <FUNCall81110000 Tag="81110000" CallDate="25/08/11" CallTime="00:03:22" TotalUsageValue="30" MeasurementUnit="1"/> I want to remove TotalUsageValue="30" only and TotalUsageValue="XXXXX" here XXX can be any value. (1 Reply)
Discussion started by: muchyog
1 Replies

8. Shell Programming and Scripting

XML Awk : Urgent

Hi All, I am a beginner to Unix. So would really appreciate if people out here can help me out. I have a XML file which has a element <NoteData> in which two values DBHA and DAKO. I need to search these in all the XML files in a directory and if found in the XML file then replace the... (3 Replies)
Discussion started by: karansachdeva
3 Replies

9. UNIX for Advanced & Expert Users

Urgent-reading a variable value

Hi, I have a text file in which , I have contents like t1=abc t2=xyz t3=awe ...... I am able to read contents of these variables in a script by . /temp.txt echo $t1 Now, what my requirement is something like this a="t" i=1 echo $a$i --->this is displaying t1 (3 Replies)
Discussion started by: kaaakrishna
3 Replies

10. Shell Programming and Scripting

Need urgent help for reading file

Hi, I am new to shell scripting. I have one .txt file whose name is status.txt Now i want to read the contents of this file in a variable. status.txt file contains the following input 1.xml 2.ps 3.pdf now i want to read this status.txt file. Can anybody please help me to write a... (2 Replies)
Discussion started by: sunitachoudhury
2 Replies
Login or Register to Ask a Question