我试图将XML解析为R数据框,这个链接帮了我很多:
但我仍然无法弄清楚我的问题:
这是我的代码:
data <- xmlParse("http://forecast.weather.gov/MapClick.php?lat=29.803&lon=-82.411&FcstType=digitalDWML")
xmlToDataFrame(nodes=getNodeSet(data1,"//data"))[c("location","time-layout")]
step1 <- xmlToDataFrame(nodes=getNodeSet(data1,"//location/point"))[c("latitude","longitude")]
step2 <- xmlToDataFrame(nodes=getNodeSet(data1,"//time-layout/start-valid-time"))
step3 <- xmlToDataFrame(nodes=getNodeSet(data1,"//parameters/temperature"))[c("type="hourly"")]
Run Code Online (Sandbox Code Playgroud)
我想要的数据框是这样的:
latitude longitude start-valid-time hourly_temperature
29.803 -82.411 2013-06-19T15:00:00-04:00 91
29.803 -82.411 2013-06-19T16:00:00-04:00 90
Run Code Online (Sandbox Code Playgroud)
我被困在了xmlToDataFrame(),任何帮助都会非常感激,谢谢.
对于最近提出的问题,我开始怀疑是否有一种非常简单的方法来处理Python中的XML文档.一种pythonic方式,如果你愿意的话.
如果我举一个例子,也许我可以解释得最好:让我们说以下 - 我认为这是一个很好的例子,说明如何(错误地)在Web服务中使用XML - 是我从http请求到http://www.google的回复的.com/IG/API?天气= 94043
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Mountain View, CA"/>
<postal_code data="94043"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-06-23"/>
<current_date_time data="2010-06-24 00:02:54 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Sunny"/>
<temp_f data="68"/>
<temp_c data="20"/>
<humidity data="Humidity: 61%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Wind: NW at 19 mph"/>
</current_conditions>
...
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="59"/>
<high data="75"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
</weather>
</xml_api_reply>
Run Code Online (Sandbox Code Playgroud)
在加载/解析这样的文档之后,我希望能够像访问那样简单地访问信息 …
我正在制作一个Sudoku Android应用程序,我在main.xml下遇到以下错误:"错误:解析XML时出错:XML或文本声明不在实体的开头"任何帮助将不胜感激.这是我的代码.我在错误旁边放了'✗'
`?<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@String/continue_label"/>
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label"/>
<Button
android:id="@+id/about_button"
android:layout_Width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label"/>
<Button
android:id="@+id/exit_button"
android:layout_Width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
`
我正在尝试创建一个dict类来处理xml但是卡住了,我真的没想到了.如果有人可以指导这个主题会很棒.
到目前为止开发的代码
class XMLResponse(dict):
def __init__(self, xml):
self.result = True
self.message = ''
pass
def __setattr__(self, name, val):
self[name] = val
def __getattr__(self, name):
if name in self:
return self[name]
return None
message="<?xml version="1.0"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
XMLResponse(message)
Run Code Online (Sandbox Code Playgroud) 如何将漏勺用于xml反序列化?文档说它甚至可以用于xml反序列化和验证,但我没有在文档或网络上找到任何好的例子!
如果有人使用漏勺进行xml反序列化,你可以在这里使用一个片段吗?这将非常有帮助.
我正在尝试在Python中将XML解析为类似于表的结构.想象一下这样的XML:
<?xml version="1.0" encoding="UTF-8"?>
<base>
<element1>element 1</element1>
<element2>element 2</element2>
<element3>
<subElement3>subElement 3</subElement3>
</element3>
</base>
Run Code Online (Sandbox Code Playgroud)
我想得到这样的结果:
KEY | VALUE
base.element1 | "element 1"
base.element2 | "element 2"
base.element3.subElement3 | "subElement 3"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用xml.etree.cElementTree,然后在这里描述的函数如何在Python中将xml字符串转换为字典?
有没有可以做到这一点的功能?我找到的所有答案都是针对特定的XML方案编写的,需要针对每个新的XML方案进行编辑.作为参考,在R中使用XML和XML2包以及xmlToList函数很容易.