[xml] [Fwd: Re: manipulating tree causes Seg fault]





-------- Original Message --------
Subject:        Re: [xml] manipulating tree causes Seg fault
Date:   Thu, 13 Aug 2009 08:55:42 -0500
From:   Nick Lang <nick lang propylon com>
To:     Michael Ludwig <mlu as-guides com>
References: <4A81E9C7 8090307 propylon com> <4A827980 2070807 as-guides com> <4A82D492 4080607 propylon com> <4A830100 2030303 as-guides com> <4A831447 2060808 propylon com> <4A83C3DD 9050102 as-guides com>



Michael, below is my code, this isn't actually causing a seg fault, but I think the segfault is a result of the pointers in memory getting all screwed up in this "small action" and then later on down the line, everything else blows up as well.

Anyway the code, and the sample xml file I'm trying to process are included in this mail

Thanks
Nick

sample.xml:
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="http://openoffice.org/2000/office"; xmlns:style="http://openoffice.org/2000/style"; xmlns:text="http://openoffice.org/2000/text"; xmlns:table="http://openoffice.org/2000/table"; xmlns:draw="http://openoffice.org/2000/drawing"; xmlns:fo="http://www.w3.org/1999/XSL/Format"; xmlns:xlink="http://www.w3.org/1999/xlink"; xmlns:dc="http://purl.org/dc/elements/1.1/"; xmlns:meta="http://openoffice.org/2000/meta"; xmlns:number="http://openoffice.org/2000/datastyle"; xmlns:svg="http://www.w3.org/2000/svg"; xmlns:chart="http://openoffice.org/2000/chart"; xmlns:dr3d="http://openoffice.org/2000/dr3d"; xmlns:math="http://www.w3.org/1998/Math/MathML"; xmlns:form="http://openoffice.org/2000/form"; xmlns:script="http://openoffice.org/2000/script"; xmlns:ooo="http://openoffice.org/2004/office"; xmlns:ooow="http://openoffice.org/2004/writer"; xmlns:oooc="http://openoffice.org/2004/calc"; xmlns:dom="http://www.w3.org/2001/xml-events"; xmlns:xforms="http://www.w3.org/2002/xforms"; xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; office:version="1.0" office:class="text">
      <office:script/>
      <office:body>
<text:p text:style-name="Flavius Paragraph Center Justified">xyz</text:p> <text:p text:style-name="Flavius Paragraph Center Justified">zabcz</text:p> <text:p text:style-name="Flavius Paragraph Center Justified">abc</text:p>
      </office:body>
</office:document-content>


peel.py

#!/usr/bin/env python
import libxml2
import sys

def peel(doc, node):
  """
  """
  children = node.get_children()
  nodelist = []
  while children:
      nodelist.append(children)
      children = children.next
removed = []
  for n in nodelist:
      n.unlinkNode()
      removed.append(n)
removed.reverse()
  for branch in removed:
      if node.get_next() != None:
          node.get_next().addPrevSibling(branch)
      else:
          node.parent.addChild(branch)

  node.unlinkNode()
  node.freeNode()
def paste_right(current_node, nodelist):
  for node in nodelist:
      current_node.addNextSibling(node)
def go(filename):
  doc = libxml2.parseFile(filename)
  root = doc.getRootElement()
  ctxt = doc.xpathNewContext()
  nodelist = ctxt.xpathEval("//text()")
xstring = """<e xmlns:text="http://openoffice.org/2000/text";>za<text:span text:style-name='bold'>b</text:span>cz</e>"""
  doc2 = libxml2.parseMemory(xstring,len(xstring))
  root2 = doc2.getRootElement()
  new_node = root2.copyNode(extended=1)
  for n in nodelist:
      if n.content == "zabcz":
          to_be_replaced = n
  paste_right(to_be_replaced, new_node)
      to_be_replaced.addNextSibling(new_node)
peel(doc,new_node)
  print "Peeling"
  to_be_replaced.unlinkNode()
  print to_be_replaced
  print doc
if __name__ == "__main__":
  go(sys.argv[1])



Michael Ludwig wrote:
Nick Lang schrieb:

Assuming we are using the same method I explained previously: If a
tree (called tree1) with a name space declaration is added to an
existing tree (called tree2), with more name spaces (including the one
from tree1). If the node, with the name space declaration from tree1
is removed, the the namespaced elements from tree1, now in tree2
should not be effected right?

I think a small example using miniature docs to illustrate this case is
in order.

A conformant implementation will work out namespace issues correctly as
long as you don't abuse the API. I seem to remember that disorder might
result if you mix createElementNS() and createElementNS().





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]