def test_designated_empty_element_tag_does_not_change_parser_behavior(self): # The designated list of empty-element tags only affects how # empty tags are presented. It does not affect how tags are # parsed--that's the parser's job. builder = LXMLTreeBuilderForXML(empty_element_tags=['bar']) soup = BeautifulSoup(builder=builder, markup="<bar>contents</bar>") self.assertEqual(str(soup), self.document_for("<bar>contents</bar>"))
def test_empty_tag_not_in_empty_element_tag_list_has_closing_tag(self): builder = LXMLTreeBuilderForXML(empty_element_tags=['bar']) soup = BeautifulSoup(builder=builder, markup="<foo />") self.assertFalse(soup.foo.is_empty_element) self.assertEqual(str(soup), self.document_for("<foo></foo>"))
def default_builder(self): return LXMLTreeBuilderForXML()
def test_designated_empty_element_tag_has_no_closing_tag(self): builder = LXMLTreeBuilderForXML(empty_element_tags=['bar']) soup = BeautifulSoup(builder=builder, markup="<bar></bar>") self.assertTrue(soup.bar.is_empty_element) self.assertEqual(str(soup), self.document_for("<bar />"))
#coding=utf8 from bs4 import CData from bs4 import BeautifulSoup from bs4.builder import LXMLTreeBuilderForXML xml = \ ''' <?xml version="1.0" ?> <foo> <bar><![CDATA[!@#$%^&*()_+{}|:"<>?,./;'[]\-=]]></bar> </foo> ''' builder = LXMLTreeBuilderForXML() soup = BeautifulSoup(xml, "xml") print(soup.new_string) soup.foo.bar.string = CData(soup.foo.bar.string) soup = soup.prettify(formatter="xml") print(soup)