在本文中,我们将详细介绍android视图中经常出现的问题,解析XML时出错:未绑定前缀的各个方面,并为您提供关于无法解析xml的相关解答,同时,我们也将为您带来关于androiddom解析xml方式
在本文中,我们将详细介绍android视图中经常出现的问题,解析XML时出错:未绑定前缀的各个方面,并为您提供关于无法解析xml的相关解答,同时,我们也将为您带来关于android dom 解析xml方式、android XPath 解析xml、android – 在Google地图片段中膨胀XML时出错、android – 此XML中的错误:解析XML时出错:未绑定的前缀的有用知识。
本文目录一览:- android视图中经常出现的问题,解析XML时出错:未绑定前缀(无法解析xml)
- android dom 解析xml方式
- android XPath 解析xml
- android – 在Google地图片段中膨胀XML时出错
- android – 此XML中的错误:解析XML时出错:未绑定的前缀
android视图中经常出现的问题,解析XML时出错:未绑定前缀(无法解析xml)
我在 android 视图中经常遇到问题,Error parsing XML: unbound prefix on Line 2
.
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" android:id="@+id/myScrollLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textandroid:gravity="center_horizontal"> </TextView> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:scrollbars="vertical"> <LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"> </LinearLayout> </ScrollView></LinearLayout>
答案1
小编典典发生这种情况的几个原因:
1) 您看到此错误的名称空间不正确,或属性中存在拼写错误。就像’xmlns’是错误的,应该是xmlns:android
2)第一个节点需要包含: xmlns:android="http://schemas.android.com/apk/res/android"
3) 如果您正在集成 AdMob,请检查自定义参数,例如ads:adSize
,您需要
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
4)如果您正在使用LinearLayout
,您可能必须定义工具:
xmlns:tools="http://schemas.android.com/tools"
android dom 解析xml方式
首先自己创建一个xml文件:DomTest.xml<?xml version=1.0 encoding=utf-8?> <classes> <group name=一年级 num=10> <person name=小明 age=7> <chinese> 语文80 </chinese> <english> 英语89 </english> </person> <person name=小强 age=8> <chinese> 语文90 </chinese> <english> 英语99 </english> </person> </group> <group name=二年级 num=20> <person name=小文 age=8> <chinese> 语文85 </chinese> <english> 英语95 </english> </person> <person name=小中 age=9> <chinese> 语文80 </chinese> <english> 英语90 </english> </person> </group> </classes>
解析出来的结果显示如下图:
下面来分析源代码:
/** * 用dom方式 解析xml 文件 * @param fileName */ private String DomXMLParse(String fileName) { String str=; // xml文档创建工厂 DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance(); // xml文档创建实例 DocumentBuilder docBuilder; // xml文档 Document doc = null; InputStream inStream = null; try { docBuilder = docFactory.newDocumentBuilder(); // 从assets文件夹下获取文件 转换成输入流 inStream = this.getResources().getAssets().open(fileName); doc = docBuilder.parse(inStream); // 获取xml跟元素 Element rootEle = doc.getDocumentElement(); // 二级父元素的list列表 NodeList groupNode = rootEle.getElementsByTagName(group); // NodeList childNode = rootEle.getElementsByTagName(person); // 遍历Classe下所有的group for (int i = 0; i < groupNode.getLength(); i++) { Element groupEle = (Element) groupNode.item(i); String groupName = groupEle.getAttribute(name); String num = groupEle.getAttribute(num); str =str+name =+groupName+ num = +num+\n; Log.e(xml, name = + groupName + num = + num); // NodeList personNode = groupNode.item(i).getChildNodes(); NodeList personNode = groupEle.getElementsByTagName(person); // 遍历group下的所有person for (int j = 0; j < personNode.getLength(); j++) { Element personEle = (Element) personNode.item(j); String name = personEle.getAttribute(name); String age = personEle.getAttribute(age); str =str+personName =+name+ personAge = +age+\n; Log.e(xml, name = + name + age = + age); Element chineseEle = (Element) personEle .getElementsByTagName(chinese).item(0); Element englistEle = (Element) personEle .getElementsByTagName(english).item(0); String chinese = chineseEle.getFirstChild().getNodeValue(); String english = englistEle.getFirstChild().getNodeValue(); str =str+chinese = +chinese+ english = +english+\n; Log.e(xml, chinese = + chinese + english = + english); } } } catch (ParserConfigurationException e1) { e1.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } catch (SAXException e) { e.printstacktrace(); } return str; }
为 XML 文档的已解析版本定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构。优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、cpu)。
android XPath 解析xml
XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 同时被构建于 XPath 表达之上。
因此,对 XPath 的理解是很多高级 XML 应用的基础。具体学习XPath参照http://www.w3school.com.cn/xpath/index.asp。
XPath只适合用来查询xml的信息,对于完整的解析xml文件的建议不要使用这个方式,最好的解析xml文件应该是sax,pull这两种方式。
我是在Android 2.2系统上做的这个测试,低于2.2不知道行不行。
下面就具体说下XPath解析xml的步骤:xpathTest.xml 和android dom 解析xml方式 中的DomTest.xml一样
1、创建InputSources
2、获得XPathFactory实例。
3、用XPathFactory实例获取XPath的实例
4、XPath调用evaluate()方法获取查询出的NodeList
private void xPathParserXml(){ //获取XPathFactory实例 XPathFactory factory = XPathFactory.newInstance(); //用工程生成XPath实例,解析xml XPath xpath = factory.newXPath(); // try { InputSource source = new InputSource(getResources().getAssets().open(xPathTest.xml)); //第一个参数:需要查询的节点名称,必须要在节点前加“//” //第二个参数:查询的输入源 //第三个参数:返回的类型 // NodeList nodeList = (NodeList) xpath.evaluate(//group, source, XPathConstants.NODESET); // if(nodeList != null && nodeList.getLength() > 0){ // for(int i = 0;i < nodeList.getLength();i++){ // Node node = nodeList.item(i); // //在这也可以得到<group>的子节点<person>。但是这样不符合xpath的风格。 // NodeList personList = node.getChildNodes(); // Element nodeAttr =(Element)node; // String groupName = nodeAttr.getAttribute(name); // String num = nodeAttr.getAttribute(num); // // Log.e(TEST, +groupName+ +num); // } // } // //获取<person>节点信息 // NodeList personList = (NodeList) xpath.evaluate(//person, source, XPathConstants.NODESET); // if(personList != null && personList.getLength() > 0){ // for(int i = 0;i < personList.getLength();i++){ // Element node = (Element)personList.item(i); // //在这也可以得到<person>的子节点<chinese>和<english>。 // NodeList childList = node.getChildNodes(); // String groupName = node.getAttribute(name); // String age = node.getAttribute(age); // // Log.e(TEST, +groupName+ +age); // } // } //获取<chinese>节点信息 NodeList chineseList = (NodeList) xpath.evaluate(//chinese, source, XPathConstants.NODESET); if(chineseList != null && chineseList.getLength() > 0){ for(int i = 0;i < chineseList.getLength();i++){ Node node = chineseList.item(i); String chinese = node.getTextContent(); Log.e(TEST, +chinese); } } } catch (IOException e) { // Todo Auto-generated catch block e.printstacktrace(); } catch (XPathExpressionException e) { e.printstacktrace(); } }
注意:xpath.evaluate()不能调用两次,报错误。至于原因不清楚。知道原因的请留言告知,谢谢。
对已有人提出XPath能不能查询很大的xml文件(超过1M或),这个在理论上应该可以,只要你能解决InputSource可以读取大容量文件问题就可以了。
android – 在Google地图片段中膨胀XML时出错
尝试使用片段显示Google地图.使用以下page作为教程.
我得到异常“错误膨胀类片段”.
1)导入jar google-play-services.jar
2)下载并配置了google play services SDK.
3)获得最新的v2 API密钥.
4)在清单中添加了com.google.android.providers.gsf.permission.READ_GSERVICES权限.
5)使用mindsdk = 8和target = 16.
作为参考,Androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.mapsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:glEsversion="0x00020000" android:required="true"/>
<permission
android:name="com.example.com.mapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.com.mapsdemo.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.com.mapsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<Meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9ipwgk"/>
</application>
MainActivity.java
package com.example.com.mapsdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
//API : AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9ipwgk
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Logcat:
01-02 18:31:21.477: I/jdwp(11473): Ignoring second debugger -- accepting and dropping
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-02 18:31:24.509: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-02 18:31:24.602: D/Openglrenderer(11871): Enabling debug mode 0
解决方法:
当您尝试使用源代码正在执行的本机API Level 11版本的片段时,通常会出现导致类片段错误的错误,但是尝试在较旧版本的Android上运行它.为了使用< fragment>在API级别10及以下版本中,您必须使用Android Support软件包的片段backport:
>将Android Support包添加到您的项目中
>继承自FragmentActivity而不是Activity
>使用SupportMapFragment而不是MapFragment
>根据此backport更改现在不同的任何其他内容
或者,您可以将android:minSdkVersion设置为11或更高,并运行当前代码,仅适用于较新的设备.
您可以在the documentation中阅读有关Android支持包的更多信息.
android – 此XML中的错误:解析XML时出错:未绑定的前缀
我解析XML时出错:此xml文件中的未绑定前缀:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sipLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:src="@drawable/connected" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"/>
</LinearLayout>
该错误位于< ImageView>中.
可能是什么问题呢?
非常感谢你.
解决方法:
您必须在布局xml的根元素中放置名称空间声明xmlns:android =“http://schemas.android.com/apk/res/android”:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" [...]
关于android视图中经常出现的问题,解析XML时出错:未绑定前缀和无法解析xml的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于android dom 解析xml方式、android XPath 解析xml、android – 在Google地图片段中膨胀XML时出错、android – 此XML中的错误:解析XML时出错:未绑定的前缀等相关内容,可以在本站寻找。
本文标签: