indent in xml

teo teo at crepido.com
Thu Aug 2 11:07:04 EDT 2001


try this one:



doc.parse(s) # s is the xmlfile...

thedoc = doc.documentElement

books = thedoc.getElementsByTagName("book") # returns a list of all
childs  #                                             with tagName ==
'Book' :)

for book in books:

  title_list = book.getElementsByTagName("title")

  print title_list[0].firstChild.data.strip()

  print book.firstChild.firstChild.data.strip()  



I think this is what you want to do.:) 



/teodor







"sdf" <wqh-2 at 263.net> wrote in message news:<mailman.996723367.12395.python-list at python.org>...



> I want to get the first child node of one node,but because of the indent 



> problem(I think so),the first two child is not what i want ,but the '\n' 



> and '',the xml file is:



> <books>



>   book>



>   <title>Pierre: The Ambiguities</title> 



>   <author>Herman Melville</author> 



>   <price>9.99</price> 



>   </book>



>   <book>



>    <title>The Republic</title> 



>   <author>Plato</author> 



>   <price>12.99</price> 



>   </book>



>   <book>



>   <title>The Republic</title> 



>   <author>Plato</author> 



>   <price>15.99</price> 



>   </book>



> </books>



> 



> and my py file is



> 



> >>> from xml.dom.minidom import parse, parseString



> >>> s="d:\\i.xml"



> >>> doc=parse(s)



> >>> doc.childNodeTypes



>  (1, 7, 8, 10)



> >>> doc.nodeName



>  '#document'



> >>> doc.childNodes



>  [<DOM Element: books at 11502084>]



> >>> book=doc.firstChild



> >>> book.childNodes



> [<DOM Text node "\n">, <DOM Text node "  ">, <DOM Element: book at
11610468>, <DOM Text node "\n">, <DOM Text node "  ">, <DOM Element:
book at 11525428>, <DOM Text node "\n">, <DOM Text node "  ">, <DOM
Element: book at 11622660>, <DOM Text node "\n">, <DOM Text node " 
">, <DOM Element: book at 11630100>, <DOM Text node "\n">, <DOM Text
node "  ">, <DOM Element: book at 11637540>, <DOM Text node "\n">,
<DOM Text node "  ">, <DOM Element: book at 11644948>, <DOM Text node
"\n">, <DOM Text node "  ">, <DOM Element: book at 11652356>, <DOM
Text node "\n">, <DOM Text node "  ">, <DOM Element: book at
11659756>, <DOM Text node "\n">, <DOM Text node "  ">, <DOM Element:
book at 11667156>, <DOM Text node "\n">]



> ###############   how can I delete the <DOM Text node "\n"> and <DOM Text node "  ">



> ##      and what is the meaning of (1, 7, 8, 10)(the reslut of doc.childNodeTypes)



> 



> 



> __________________________________________



> 



> DVD´óƬ£¬Ò»ÂÉ10Ôª  http://shopping.263.net/category02.htm



> ÃÀÈÝÑøÑÕ¡¢¼õ·ÊÊÝÉíÃؾ÷  http://shopping.263.net/category10.htm



More information about the Python-list mailing list