我很难找到一个如何使用Element Tree在python中解析XML的基本示例.根据我的发现,这似乎是用于解析XML的最简单的库.以下是我正在使用的XML示例:
<timeSeriesResponse>
<queryInfo>
<locationParam>01474500</locationParam>
<variableParam>99988</variableParam>
<timeParam>
<beginDateTime>2009-09-24T15:15:55.271</beginDateTime>
<endDateTime>2009-11-23T15:15:55.271</endDateTime>
</timeParam>
</queryInfo>
<timeSeries name="NWIS Time Series Instantaneous Values">
<values count="2876">
<value dateTime="2009-09-24T15:30:00.000-04:00" qualifiers="P">550</value>
<value dateTime="2009-09-24T16:00:00.000-04:00" qualifiers="P">419</value>
<value dateTime="2009-09-24T16:30:00.000-04:00" qualifiers="P">370</value>
.....
</values>
</timeSeries>
</timeSeriesResponse>
Run Code Online (Sandbox Code Playgroud)
我能够使用硬编码方法做我需要的事情.但我需要我的代码更有活力.这是有效的:
tree = ET.parse(sample.xml)
doc = tree.getroot()
timeseries = doc[1]
values = timeseries[2]
print child.attrib['dateTime'], child.text
#prints 2009-09-24T15:30:00.000-04:00, 550
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的一些事情,其中没有一个有效,报告说他们找不到时间序列(或我尝试过的任何其他内容):
tree = ET.parse(sample.xml)
tree.find('timeSeries')
tree = ET.parse(sample.xml)
doc = tree.getroot()
doc.find('timeSeries')
Run Code Online (Sandbox Code Playgroud)
基本上,我想加载xml文件,搜索timeSeries标记,并遍历值标记,返回dateTime和标记本身的值; 我在上面的例子中所做的一切,但不是硬编码我感兴趣的xml部分.有人能指出我的一些例子,或者给我一些关于如何解决这个问题的建议?
谢谢你的帮助.使用以下两个建议对我提供的示例文件起作用,但是,它们不能在完整文件上工作.这是我使用Ed Carrel的方法从真实文件中得到的错误:
(<type 'exceptions.AttributeError'>, AttributeError("'NoneType' object has no attribute 'attrib'",), <traceback object at 0x011EFB70>)
Run Code Online (Sandbox Code Playgroud)
我认为在它不喜欢的真实文件中有一些东西,所以我逐渐删除了东西直到它工作.以下是我更改的行: …
在我的PhoneGap/jQuery Mobile应用程序中,我目前正在使用PhoneGaps的Camera API来允许用户拍摄照片,该照片也存储在相机胶卷中.我将DestinationType设置为FILE_URI并将此路径保存在本地数据库中.但是,FILE_URI是应用程序关闭时销毁的临时位置的路径.我在考虑保存图像的名称,然后从相机胶卷中检索图像.是否有一条路径可用于稍后检索相机胶卷中的图像?
我在db中将图像保存为Base64,但由于PhoneGap API Doc中的这个注释,我对这种方法有点厌倦:
注意:在较新设备上使用相机拍摄的照片的图像质量非常好.使用Base64对这些图像进行编码会导致某些设备(iPhone 4,BlackBerry Torch 9800)出现内存问题.因此,强烈建议使用FILE_URI作为'Camera.destinationType'.
编辑:
得到了@Simon MacDonald的回答.这是我的精简代码:
function capturePhoto() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType: Camera.DestinationType.FILE_URI });
}
function onPhotoURISuccess(imageURI) {
createFileEntry(imageURI);
}
function createFileEntry(imageURI) {
window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);
}
function copyPhoto(fileEntry) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
fileSys.root.getDirectory("photos", {create: true, exclusive: false}, function(dir) {
fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail);
}, fail);
}, fail);
}
function onCopySuccess(entry) {
console.log(entry.fullPath)
}
function fail(error) {
console.log(error.code);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用PHP从MySQL数据库中提取数据.我能够使用DOM函数构建XML文件.然后使用 echo $dom->saveXML(); ,我能够从AJAX调用返回XML.而不是使用AJAX来获取XML,我如何将XML文件保存到服务器上的某个位置?谢谢
我有一个python脚本,使用PIL将文本写入图像.这一切都很好,除非我遇到带有回车符的字符串.我需要在文本中保留回车符.而不是将回车写入图像,我得到一个小盒子字符,返回应该是.以下是编写文本的代码:
<code>
draw = ImageDraw.Draw(blankTemplate)
draw.text((35 + attSpacing, 570),str(attText),fill=0,font=attFont)
</code>
Run Code Online (Sandbox Code Playgroud)
attText是我遇到麻烦的变量.我在写它之前将它转换为字符串,因为在某些情况下它是一个数字.
谢谢你的帮助.