我有一个简单的要求,我需要获取属性的值,xml:id即af1.我正在使用a SAXParser而这里是我xpath:a/aff/@xml:id的相反我能够获取使用的值xpath:a/aff/@value.
但我无法找回价值,请你帮忙吗?
<?xml version="1.0" encoding="UTF-8" ?>
<a>
<aff xml:id="af1" value="a">
<uAff>
Hello
</uAff>
</aff>
<aff xml:id="corr1">
<uAff>
Hello1
</uAff>
</aff>
</a>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在尝试解析android上的xml文档时,我收到"SAXParseException:意外的文档结束"错误.
有问题的文件来自google weather api,但无论有问题的xml文件(只要xml有效),似乎都会抛出相同的错误,所以我怀疑这是我的方法的问题,而不是xml.
这是作为一个学习练习,所以我可能(希望)忽略了一些明显的东西=)
我通过在线验证器运行xml,然后它恢复正常.(不能告诉我它是否有效,因为我没有DTD,但我认为我不需要DTD来解析xml).
这是我用来尝试解析文件的代码:
private void refreshForecast()
URL url;
try {
url = new URL( "http://192.168.1.66:8000/google4.xml");
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// falls over here parsing the xml.
Document dom = db.parse(in);
}
} catch (ManyExceptions e) {
....
}
Run Code Online (Sandbox Code Playgroud)
产生错误的xml的缩减版本是:
<?xml version="1.0"?>
<xml_api_reply version="1">
<weather>
<forecast_information>
<city>Hamilton</city>
</forecast_information>
</weather>
</xml_api_reply>
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪是:
11-20 …Run Code Online (Sandbox Code Playgroud) 我正在寻找SAX和Pull Parser之间的主要区别.我知道SAX解析器适合处理大型XML文件,因为它不存储XML并且只在一个方向上遍历.与DOM相比.但我无法找到SAX和PULL之间的主要区别.请建议我任何链接
当我尝试使用Java解析XML文件(在GAE服务器中)时,我有时会遇到以下错误:
Parse: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 3; The element type "META" must be terminated by the matching end-tag "</META>".
Run Code Online (Sandbox Code Playgroud)
然而它并没有一直发生,有时它的工作正常.该程序解析xml文件,我没有问题.
这是我试图解析的XML文件:http: //www.fulhamchronicle.co.uk/london-chelsea-fc/rss.xml
任何帮助将不胜感激.谢谢.
更新:
谢谢你的回答.我将我的代码更改为另一个解析器,以及该文件正在正确解析的好消息.它现在转移到另一个饲料相同的问题,尽管完全不同的饲料相同的线,它之前完美的工作.谁能想到为什么会这样?
我正在尝试使用SAX解析器project1从下面的示例xml文档(原始文档大约30 GB)中删除所有节点(以及它们的子元素).如果有一个单独的修改文件或者可以使用in-行编辑.
sample.xml
<ROOT>
<test src="http://dfs.com">Hi</test>
<project1>This is old data<foo></foo></project1>
<bar>
<project1>ty</project1>
<foo></foo>
</bar>
</ROOT>
Run Code Online (Sandbox Code Playgroud)
这是我的尝试..
parser.py
from xml.sax.handler import ContentHandler
import xml.sax
class MyHandler(xml.sax.handler.ContentHandler):
def __init__(self, out_file):
self._charBuffer = []
self._result = []
self._out = open(out_file, 'w')
def _createElement(self, name, attrs):
attributes = attrs.items()
if attributes:
out = ''
for key, value in attributes:
out += ' {}={}'.format(key, value)
return '<{}{}>'.format(name, out)
return '<{}>'.format(name)
def _getCharacterData(self):
data = ''.join(self._charBuffer).strip()
self._charBuffer = []
self._out.write(data.strip()) #remove strip() …Run Code Online (Sandbox Code Playgroud) 我需要解析潜在的巨大XML文件,所以我猜这会排除DOM解析器.
有没有适合C++的轻量级SAX解析器,与足迹上的TinyXML相比?XML的结构非常简单,不需要像名称空间和DTD这样的高级内容.只是元素,属性和cdata.
我知道Xerces,但它超过50mb的超大尺寸让我颤抖.
谢谢!
我正在使用Xerces来解析我的xml文档.问题是xml转义了像' '这样的字符 在characters()方法中显示为非转义的.我需要在characters()方法中获取转义字符.
谢谢.
UPD:尝试覆盖我的DefaultHandler后代的resolveEntity方法.从调试中可以看出它被设置为xml阅读器的实体解析器,但是没有调用来自重写方法的代码.
您好我试图解析包含dict数组的plist文件.我试图用xmlwise来做这件事.plistfile的内容在这里
到目前为止,我只在我的活动中有这个和我获取plistfile的内容,但如何将内容解析为arraylist?
Map<String, Object> properties = null;
try {
InputStream inputStream = getResources().openRawResource(R.raw.first_5);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
properties = Plist.fromXml(sb.toString());
// TODO do something with the object here
Log.v("--", properties.values() + " " + properties.size());
Log.v("--", "OB "+properties.get());
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个使用SAX从互联网上检索数据的应用程序.之前我用它来解析像Google Weather API这样的简单XML文件.但是,我感兴趣的网站会将解析提升到一个新的水平.页面很大,看起来很乱.我只需要检索一些特定的行; 其余的对我没用.
有可能跳过那些无用的线/标签,还是我必须一步一步走?
我正在从SD卡读取xml文件.在这里,我想更改XML文件的值,我想将文件保存到SD卡.
我的代码如下所示....请指导我在更新值后如何将XML文件保存到SD卡.
public void modifyNodeval(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("/sdcard/sss.xml"));
//Get the staff element by tag name directly
Node nodes = doc.getElementsByTagName("Employee").item(0);
//loop the staff child node
NodeList list = nodes.getChildNodes();
for (int i =0; i<list.getLength();i++){
Node node = list.item(i);
//get the salary element, and update the value
if("Emp_Name".equals(node.getNodeName())){
node.setNodeValue("795796");
}
}
Run Code Online (Sandbox Code Playgroud)