Issue updating BLOB field with XML bytearray

3733
4
03-24-2014 08:18 AM
by Anonymous User
Not applicable
Original User: iggz

I have a script that uses an Update Cursor to update a BLOB field with XML if certain criteria are met.

from lxml import etree
import arcpy

data = open("C:/data.txt", "r").readlines()

with arcpy.da.Editor(workspace) as edit:
    with arcpy.da.UpdateCursor(Point, ["ID", "Blob"]) as cursor:
        for row in cursor:

            root = etree.Element("Root")

            for line in data:
                line_split = line.split("|")
                
                if line_split[0] == str(row[0]):

                    one = etree.SubElement(root, "one")
                    two = etree.SubElement(one, "two").text = "two"
                    three = etree.SubElement(one, "three").text = "three"
                    four = etree.SubElement(one, "four").text = "four"
                    five = etree.SubElement(one, "five").text = "five"
                    six = etree.SubElement(one, "six").text = "six"
                    seven = etree.SubElement(one, "seven").text = "seven"
                    eight = etree.SubElement(one, "eight").text = "eight"
                    nine = etree.SubElement(one, "nine").text = "nine"
                    ten = etree.SubElement(one, "ten").text = "ten"
                    eleven = etree.SubElement(one, "eleven").text = "eleven"
                    twelve = etree.SubElement(one, "twelve").text = "twelve"
                    thirteen = etree.SubElement(one, "thirteen")
                    fourteen = etree.SubElement(one, "fourteen").text = "fourteen"
                    fifteen = etree.SubElement(one, "fifteen")

            data = bytearray(etree.tostring(root, encoding='UTF-8', pretty_print=True), "UTF-8")
            
            #The XML in this file shows correctly
            open("C:/XML" + os.sep + str(ID) + ".txt", "w").write(data)
            
            row[1] = data
            cursor.updateRow(row)

           
           
Then I read the BLOB field:

with arcpy.da.SearchCursor(Point, ["OBJECTID", "Blob"]) as cursor:
    for row in cursor:
        fileName = row[0]
        binaryRep = row[1]
        open("C:/XML" + os.sep + str(fileName) + ".xml", 'wb').write(binaryRep.tobytes())

 
And when I view the XML from the BLOB field it's all messed up like this:

[ATTACH=CONFIG]32437[/ATTACH]

But when I view the text file I also created right before row[1] = data, the XML is correct:

[ATTACH=CONFIG]32438[/ATTACH]

Anyone know where I'm going wrong here?

PS-
What can I buy with all my points?
0 Kudos
4 Replies
JamesCrandall
MVP Frequent Contributor
Relevant info at the bottom of the reference page with example that may help you figure it out

http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000
0 Kudos
by Anonymous User
Not applicable
Original User: iggz

Relevant info at the bottom of the reference page with example that may help you figure it out

http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000


Yeah, I've seen that, but I don't really see how it helps unless I'm missing something obvious.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Yeah, I've seen that, but I don't really see how it helps unless I'm missing something obvious.


Not sure.

off topic: you must have helped a lot of members to get >4 billion points!  wow!
0 Kudos
by Anonymous User
Not applicable
Original User: iggz

Bump it up
0 Kudos