使用SAX方式解析Xml文件
程序员文章站
2022-05-28 08:32:13
...
xml 文件如下:
解析类的ContentHandler
RCP视图使用TreeViewer,其内容提供类与标签提供类如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<structure>
<name value="Internet Prototal"></name>
<tiptext>dd</tiptext>
<member>
<name value="length"></name>
<tiptext>ddf</tiptext>
</member>
<union>
<name value="dkdkdk"></name>
<member>
<name value="icmp"></name>
<tiptext>ddf</tiptext>
</member>
</union>
</structure>
<structure>
<name value="Internet Version 6"></name>
<tiptext>dd</tiptext>
<member>
<name value="icmp"></name>
<tiptext>ddf</tiptext>
</member>
</structure>
</root>
解析类的ContentHandler
package com.example.rcp.first.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.example.rcp.first.model.DefaultXmlNode;
import com.example.rcp.first.model.IXmlNode;
import com.example.rcp.first.model.Jump;
import com.example.rcp.first.model.Member;
import com.example.rcp.first.model.Structure;
import com.example.rcp.first.model.Union;
public class MyContentHandler extends DefaultHandler {
IXmlNode root = null;
public IXmlNode getRoot() {
return root;
}
public void setRoot(IXmlNode root) {
this.root = root;
}
IXmlNode parent = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(root == null){
root = new DefaultXmlNode();
return;
}
DefaultXmlNode cNode = new DefaultXmlNode();
if("structure".equals(qName)){
cNode = new Structure();
if(parent == null){
root.addChild(cNode);
}else{
parent.addChild(cNode);
}
}else if("union".equals(qName)){
cNode = new Union();
if(parent != null){
parent.addChild(cNode);
}
}else if("member".equals(qName)){
cNode = new Member();
if(parent != null){
parent.addChild(cNode);
}
}else if("jump".equals(qName)){
cNode = new Jump();
if(parent != null){
parent.addChild(cNode);
}
}else{
if("name".equals(qName)){
if(parent!=null){
parent.setName(attributes.getValue("value"));
}
}
}
cNode.setParent(parent);
String pName = parent == null ? "" : (parent.getClass() + "@" + parent.getName());
//System.out.println("当前结点:" + qName + " 父结点:" + pName);
parent = cNode;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(parent != null)
parent = parent.getParent();
}
}
RCP视图使用TreeViewer,其内容提供类与标签提供类如下:
class ViewContentProvider implements IStructuredContentProvider,
ITreeContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
System.out.println(newInput);
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if(parent instanceof IXmlNode){
IXmlNode node = (IXmlNode)parent;
return node.getChildren().toArray();
}
else{
return null;
}
}
public Object getParent(Object child) {
return null;
}
public Object [] getChildren(Object parent) {
if (parent instanceof IXmlNode) {
return ((IXmlNode)parent).getChildren().toArray();
}
return new Object[0];
}
public boolean hasChildren(Object parent) {
System.out.println(parent.getClass());
if (parent instanceof IXmlNode){
List children = ((IXmlNode)parent).getChildren();
System.out.println(children.isEmpty());
return !children.isEmpty();
}
return false;
}
}
class ViewLabelProvider extends LabelProvider {
public String getText(Object obj) {
if(obj instanceof IXmlNode){
return ((IXmlNode)obj).getName();
}
return obj.toString();
}
public Image getImage(Object obj) {
String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
if (obj instanceof IXmlNode)
imageKey = ISharedImages.IMG_OBJ_FOLDER;
return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
}
}
上一篇: ubuntu下MySQL如何开启远程连接
推荐阅读