如何用sax划分内部XML标签
我正在尝试使用SAX从XML文件中读取数据。但是我无法弄清楚如何处理内部< path>
标签...
如何进行内部路径元素?
这是我的XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<files>
<backup isActive="true">
<Path>way/to/backup</Path>
</backup>
<config>
<Path>way/to/config</Path>
</config>
<data>
<Path>way/to/data</Path>
</data>
</files>
</settings>
这里是必要的Java代码片段。
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("backup")) {
Boolean isActive = Boolean.valueOf(attributes.getValue("isActive"));
System.out.println("isActive : " + isActive);
Settings.Backup.setEnabled(isActive);
hasBackup = true;
} else if (qName.equalsIgnoreCase("config")) {
hasConfig= true;
} else if (qName.equalsIgnoreCase("data")) {
hasData = true;
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (hasBackup) {
System.out.println("Backup: "
+ new String(ch, start, length));
hasBackup = false;
} else if (hasConfig) {
System.out.println("Config: " + new String(ch, start, length));
hasConfig = false;
} else if (hasData) {
System.out.println("Data: " + new String(ch, start, length));
hasData = false;
}
}
我不想添加以下代码是正确的方法...
else if (qName.equalsIgnoreCase("path")) {
System.out.println("path");
...
}
我想我必须以某种方式触发新的Qname ...但是我不知道如何。比我只需检查备份,而在备份中,我还要运行一个
if (qName.equalsIgnoreCase("backup")) {
Boolean isActive = Boolean.valueOf(attributes.getValue("isActive"));
System.out.println("isActive : " + isActive);
Settings.Backup.setEnabled(isActive);
hasBackup = true;
trigger next qName?
if(qname.equalsIgnorCase("path") {
Settings.Backup.setPath(path)
}
}
,我还问自己是否应该在开始启动或字符方法内处理属性。
I am trying to read data from an XML file by using SAX. But i cant figure out how to handle the inner <Path>
Tag...
How to do pars the inner path element?
This is my XML file.
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<files>
<backup isActive="true">
<Path>way/to/backup</Path>
</backup>
<config>
<Path>way/to/config</Path>
</config>
<data>
<Path>way/to/data</Path>
</data>
</files>
</settings>
Here the neccessary java code snippets.
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("backup")) {
Boolean isActive = Boolean.valueOf(attributes.getValue("isActive"));
System.out.println("isActive : " + isActive);
Settings.Backup.setEnabled(isActive);
hasBackup = true;
} else if (qName.equalsIgnoreCase("config")) {
hasConfig= true;
} else if (qName.equalsIgnoreCase("data")) {
hasData = true;
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (hasBackup) {
System.out.println("Backup: "
+ new String(ch, start, length));
hasBackup = false;
} else if (hasConfig) {
System.out.println("Config: " + new String(ch, start, length));
hasConfig = false;
} else if (hasData) {
System.out.println("Data: " + new String(ch, start, length));
hasData = false;
}
}
I dont feel like adding the following code is the right approach at all ...
else if (qName.equalsIgnoreCase("path")) {
System.out.println("path");
...
}
I think i have to trigger new qName somehow ... but i dont know how. Than i would just check for backup and than inside the backup i would than run an
if (qName.equalsIgnoreCase("backup")) {
Boolean isActive = Boolean.valueOf(attributes.getValue("isActive"));
System.out.println("isActive : " + isActive);
Settings.Backup.setEnabled(isActive);
hasBackup = true;
trigger next qName?
if(qname.equalsIgnorCase("path") {
Settings.Backup.setPath(path)
}
}
Also i am asking myself if i should allready handle Attributes inside the startElement or at the characters method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在样式上:
数组
变量和方法名称是带有小字母的骆驼盒。
凹痕:4个空间
可能是嵌套块较少的措施。
原始类型布尔值,int,双重双重包装器布尔,整数,double。
某些问题:`人物可以多次称为一次,仅提供部分文本文章。 开始事件的解析很累
开始收集角色的
:您也需要结束:
On style:
Arrays
Variable and method names are camel case with a starting small letter.
Indentation: 4 spaces
Probably a measure to have less nested blocks.
Primitive types boolean, int, double better than Object wrappers Boolean, Integer, Double.
Some issues: `characters can be called more than once, deliver just a partial text piece. The parsing with startElement events are tiresome
Now to collect characters:
You'll need endElement too: