2012-09-07 1 views
2

xml 파일이 있고이 파일에 새 노드를 추가하는 데 python 스크립트가 사용됩니다. xml.dom.minidom 모듈을 사용하여 xml 파일을 처리했습니다. 파이썬 모듈로 처리 한 후 누군 데 XML 파일 국지적 변화가 첫 번째 행 후도 마지막 행과 '& quot'전에 개행 문자이다보다 실제로 주어진 바와 같이 필요로했던xml.dom.minidom python을 사용하여 xml 파일을 작성하는 경우의 문제

<?xml version="1.0" ?><Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
<PostBuildEvent> 
    <Command>xcopy &quot;SourceLoc&quot; &quot;DestLoc&quot;</Command> 
</PostBuildEvent> 
<ImportGroup Label="ExtensionTargets"> 
</ImportGroup> 
<Import Project="project.targets"/></Project> 

이하 주어진다 "

<?xml version="1.0" ?> 
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
<PostBuildEvent> 
    <Command>xcopy "SourceLoc" "DestLoc"</Command> 
</PostBuildEvent> 
<ImportGroup Label="ExtensionTargets"> 
</ImportGroup> 
<Import Project="project.targets"/> 
</Project> 

사용 된 파이썬 코드 내가,

덕분에 올바른 형식으로 XML을 얻기 위해 내 코드에 minidom의 문서에서

답변

1

를 업데이트해야합니까

xmltree=xml.dom.minidom.parse(xmlFile) 
for Import in Project.getElementsByTagName("Import"): 
    newImport = xml.dom.minidom.Element("Import") 
    newImport.setAttribute("Project", "project.targets") 
vcxprojxmltree.writexml(open(VcxProjFile, 'w')) 

아래와 같습니다 :

Node.toprettyxml([indent=""[, newl=""[, encoding=""]]]) 

Return a pretty-printed version of the document. indent specifies the indentation string and defaults to a tabulator; newl specifies the string emitted at the end of each line and defaults to \n. 

모든 사용자 정의의 당신 minidom에서 나옵니다.

텍스트 노드를 개행의 루트 형제로 삽입하려고했습니다. 희망은 오래 가지 않습니다. re 모듈의 정규식을 사용하고 수동으로 삽입하는 것이 좋습니다. 다시 모든 개체 명 이름과 해당 코드 포인트가있는 한 재를 사용하여 수동으로이 작업을 수행 할 수 있습니다, 또는

import HTMLParser 
h = HTMLParser.HTMLParser() 
unicode_string = h.unescape(string_with_entities) 

: SGML 엔티티를 제거으로

은 분명히 그 파이썬 표준 라이브러리에 대한 문서화되지 않은 기능이있다 htmlentitydefs 모듈 안에 있습니다.

관련 문제