コード例 #1
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
 def testRemoveNode(self):
     # create a tree then remove the child
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.removeNode("/root/child")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root/>\n')
     
     # create a tree then remove the second child
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.addNode("/root/child")
     doc.removeNode("/root/child[2]")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/></root>\n')
コード例 #2
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
 def testRemoveNodeNs(self):
     namespaces = {'t': "test", 's': "test2"}
     
     # Test deleting a node that's in a namespace                
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     
     doc.addNode("/root/t:child")
     doc.removeNode("/root/t:child")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root xmlns:t="test"/>\n')
     
     # create a tree then remove the second child
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     
     doc.addNode("/root/t:child")
     doc.addNode("/root/t:child")
     doc.removeNode("/root/t:child[2]")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root xmlns:t="test"><t:child/></root>\n')
コード例 #3
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
 def testInsertNodeAfter(self):
     # create a tree and then insert a new empty node
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.insertNodeAfter("/root/child", "newChild")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><newChild/></root>\n')
     
     # create a tree and then insert a new node with content
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.insertNodeAfter("/root/child", "newChild", "text")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><newChild>text</newChild></root>\n')
     
     # create a tree and then insert a new node with markup content
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.insertNodeAfter("/root/child", "newChild", "<b>text</b>", 1)
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><newChild><b>text</b></newChild></root>\n')
コード例 #4
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
 def testAddNode(self):
     # Test creating a new document
     doc = XMLFragment()
     
     doc.addNode("/root")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root/>\n')
     
     # Test adding on a child node
     doc.addNode("/root/child")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/></root>\n')
     
     # Test creating the parent and child in one step.                
     doc = XMLFragment()
     doc.addNode("/root/child")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/></root>\n')
     
     # Test creating the parent and child in one step with some content               
     doc = XMLFragment()
     doc.addNode("/root/child", "text")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text</child></root>\n')
     
     # Now add another level
     doc.addNode("/root/child/subchild")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text<subchild/></child></root>\n')
     
     # Add a second child with the same name
     doc.addNode("/root/child", "text")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text<subchild/></child><child>text</child></root>\n')
     
     # Create two children then add a subchild to each
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.addNode("/root/child")
     doc.addNode("/root/child/subchild")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><subchild/></child><child><subchild/></child></root>\n')
     
     # Create two children then add a subchild to just the second child
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.addNode("/root/child")
     doc.addNode("/root/child[2]/subchild")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><child><subchild/></child></root>\n')
     
     # Test adding with an anonymouse root node
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.addNode("/node()/child")
     doc.addNode("/node()/child[2]/subchild")
     
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><child><subchild/></child></root>\n')
     
     # add an attribute to the first child
     doc.addNode("/root/child[1]/@attr", "text")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child attr="text"/><child><subchild/></child></root>\n')
     
     # add a string that should be parsed as markup
     doc = XMLFragment()
     doc.addNode("/root/child")
     doc.addNode("/root/markup", "<test>text</test>", 1)
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><markup><test>text</test></markup></root>\n')
    
     # add a pre-parsed node to the document
     doc = XMLFragment()
     doc.addNode("/root/child")
     markup = libxml2.parseDoc("<test>text</test>")
     doc.addNode("/root/markup", markup.getRootElement())
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child/><markup><test>text</test></markup></root>\n')
コード例 #5
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
   def testUpdateNode(self):
       # create a tree and then set a node to a new value
       doc = XMLFragment()
       doc.addNode("/root/child")
       doc.updateNode("/root/child", "text")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text</child></root>\n')
       
       # add another child and then update both to the same value
       doc.addNode("/root/child")
       doc.updateNode("/root/child", "newText")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>newText</child></root>\n')
       
       # Now just update the second child.
       doc.updateNode("/root/child[2]", "text")
       
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>text</child></root>\n')
       
       # Now update an attribute
       doc.addNode("/root/child[1]/@attr", "text")
       doc.updateNode("/root/child[1]/@attr", "newText")
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child attr="newText">newText</child><child>text</child></root>\n')
       
       # change a node to a string that should be parsed as markup
       doc = XMLFragment()
       doc.addNode("/root/child", "text")
       doc.updateNode("/root/child", "<test>text</test>", 1)
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
       
       # replace the content of a node with more then one child
       doc = XMLFragment()
       doc.addNode("/root/child", "<test>text</test><test>text2</test>", 1)
       doc.updateNode("/root/child", "<test>text3</test><test>text4</test>", 1)
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text3</test><test>text4</test></child></root>\n')
      
       # add a pre-parsed node to the document
       doc = XMLFragment()
       doc.addNode("/root/child")
       markup = libxml2.parseDoc("<test>text</test>")
       doc.updateNode("/root/child", markup.getRootElement())
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
 
       # Update a node that doesn't already exist.
       doc = XMLFragment()
       markup = libxml2.parseDoc("<test>text</test>")
       doc.updateNode("/root/child", markup.getRootElement())
       self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child><test>text</test></child></root>\n')
コード例 #6
0
ファイル: TestXMLFragment.py プロジェクト: kstaken/Syncato
 def testAddNodeNs(self):        
     # Check creating the root in a namespace                
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     doc.addNode("/t:root")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test"/>\n')
     
     # now add a child in the same namespace
     doc.addNode("/t:root/t:child")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test"><t:child/></t:root>\n')
     
     # now add a child in no namespace
     doc.addNode("/t:root/child")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test"><t:child/><child/></t:root>\n')
     
     # now add a child in a different namespace
     doc.addNode("/t:root/child/s:child")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test"><t:child/><child xmlns:s="test2"><s:child/></child></t:root>\n')
     
     # Check creating the root in a namespace with some content               
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     doc.addNode("/t:root", "test")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test">test</t:root>\n')
     
     # Now add a node with no NS and some content
     doc.addNode("/t:root/child", "test")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test">test<child>test</child></t:root>\n')
     
     # Now add some markup that doesn't have any namespaces
     doc.addNode("/t:root/child/markup", "some <b>content</b> bold", 1)
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test">test<child>test<markup>some <b>content</b> bold</markup></child></t:root>\n')
     
     # Test combinations of namespaces in markup
     
     # Add some markup to a node in a namespace but where the markup has no 
     # namespaces
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     doc.addNode("/t:root")        
     doc.addNode("/t:root/s:markup", "some <b>content</b> bold", 1)
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test" xmlns:s="test2"><s:markup>some <b>content</b> bold</s:markup></t:root>\n')
     
     # Add markup that uses namespaces
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     doc.addNode("/t:root")        
     doc.addNode("/t:root/s:markup", "some <x:b xmlns:x='test3'>content</x:b> bold", 1)
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test" xmlns:s="test2"><s:markup>some <x:b xmlns:x="test3">content</x:b> bold</s:markup></t:root>\n')       
     
     # Test creation of a deeper tree to make sure we get the proper ns defs
     doc = XMLFragment()
     #doc.registerNamespaces(namespaces)
     doc.addNode("/t:root/t:child/t:child", "test")        
     doc.addNode("/t:root/t:child")
     self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<t:root xmlns:t="test"><t:child><t:child>test</t:child></t:child><t:child/></t:root>\n')