-3

嘿,我想用 java 读写 xml 代码,所以我用谷歌搜索了怎么做,但我发现只有引擎才能读取真正简单的 xml 代码,例如

<?xml version="1.0"?>
<company>
    <staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

但我想阅读更高级的内容,例如:

<?xml version="1.0" encoding="UTF-8"?>
<package>
    <template name="store_avatars">

        <!-- UI validation for this file -->
        <panel noclick="1" onload="CreateBool('ui_avatars_package_loaded', true);"/>

        <!-- Female Pyromancer -->  
        <instance name="altAvatarPreviewPanel"
            heroEntryID="1"
            id="15"
            product="Hero_Pyromancer.Female"
            definition="/heroes/pyromancer"
            heroName="Hero_Pyromancer"
            hero_icon_path="/heroes/pyromancer/icon.tga"
            hasvoice="true"
            hasmodel="true"
            hastexture="true"
            hassounds="true"
            hasanimations="true"
            haseffects="false"
        />

        <!-- Sexy Moon Queen -->
        <instance name="altAvatarPreviewPanel"
            heroEntryID="2"
            id="16"
            product="Hero_Krixi.Sexy"
            definition="/heroes/krixi"
            heroName="Hero_Krixi"
            hero_icon_path="/heroes/krixi/icons/hero.tga"
            hasvoice="false"
            hasmodel="true"
            hastexture="true"
            hasanimations="true"
            haseffects="false"
        />
    </template>
</package>

所以我继续为此做了我自己的课程,

package Lindholm.languages;

import java.util.Vector;

import Lindholm.LLException;
import Lindholm.LLString;
import Lindholm.com.LLProperty;

public class Xml {
    //STATIC variables;

    //Variables;
    private    Tag tag;
    //Setup;

    //Constructor;
    public Xml(String xml) {
        int index1;

        //First removing all the comments so they dont' disturb the decoding;
        index1 = 0;
        while((index1 = xml.indexOf("<!--",index1)) != -1) {
            int index2;

            if((index2 = xml.indexOf("-->",index1)) != -1) {
                String comment = xml.substring(index1,index2+"-->".length());
                xml = LLString.replace(xml,comment,"",1);
            }
            else {
                try {
                    throw new Exception("Invail xml code, missing \"-->\".");
                } catch (Exception e) {
                    new LLException(e);
                }
            }
        }

        //replacing all "/>" cancelings to "</[abc]>";
        index1 = 0;
        while((index1 = xml.indexOf("/>",0)) != -1) {
            int index2;

            String revstr = LLString.reverse(xml.substring(0,index1+"/>".length()));
            index2 = revstr.indexOf("<",0);
            index2 = revstr.length()-index2;
            String name = xml.substring(index2,index1).split("\\s")[0];

            xml = LLString.replace(xml,"/>","></"+name+">",1);
        }

        //Adding index's to all tags which will make the decoding easier.
        index1 = 0;
        int n = 0;
        Vector<Integer> openings = new Vector<Integer>();
        while(true) {
            int index0 = index1;
            int index2;
            n++;

            index1 = xml.indexOf("<",index0);
            index2 = xml.indexOf("</",index0);
            if(index1 != -1) {
                index1 += "<".length();
            }

            if(index1 != -1 && (index1 < index2 || index2 == -1)) {
                xml = xml.substring(0,index1)+n+"-"+xml.substring(index1);
                openings.add(n);
                index1 += (n+"-").length();
            }
            else if(index2 != -1 && (index2 < index1 || index1 == -1)) {
                xml = xml.substring(0,index2+"</".length())+openings.get(openings.size()-1)+"-"+xml.substring(index2+"</".length());
                index1 += (openings.get(openings.size()-1)+"-").length();
                openings.remove(openings.size()-1);
            }
            else {
                break;
            }
        }

        //Now let's decode it!!!
        xml = xml+"</1-?xml>";
        tag = readTag(xml,"1-?xml");
    }
    //Set;

    //Get;
    public Tag getTag() {
        return tag;
    }

    //Add;

    //Remove;

    //Do;

    //Other;
    private Tag readTag(String xmltag,String tagname) {
        int index1 = ("<"+tagname).length(); //subract 1 due the first index is 0!
        int index2;
        String body = xmltag.substring(xmltag.indexOf(">",0)+">".length(),xmltag.indexOf("</"+tagname,0));
        LLProperty properties;
        Vector<Tag> children = new Vector<Tag>();

        index2 = xmltag.indexOf(">",index1);
        String xmlproperties = xmltag.substring(index1,index2);
        properties = readProperties(xmlproperties);

        while((index1 = body.indexOf("<",0)) != -1) {
            index2 = body.indexOf(">",index1);
            String subtagname = body.substring(index1+"<".length(),index2).split("\\s")[0];

            index2 = body.indexOf("</"+subtagname,index1)+"</".length();
            index2 = body.indexOf(">",index2)+">".length();

            String subxmltag = body.substring(index1,index2);
            body = LLString.replace(body,subxmltag,"",1);

            children.add(readTag(subxmltag,subtagname));
        }
        if(children.size() == 0) {
            body = null;
        }
        tagname = tagname.split("-")[1];

        Tag tag = new Tag(tagname,body);
        tag.setProperties(properties);
        tag.setChildren(children);
        return tag;
    }
    private LLProperty readProperties(String xmlproperties) {
        LLProperty properties = new LLProperty();
        int index1 = 0;
        int index2;

        while(xmlproperties.substring(index1).contains("=")) {
            index2 = xmlproperties.indexOf("=",index1);
            String key = LLString.trimAll(xmlproperties.substring(index1,index2));
            key = key.trim();

            index1 = index2+"=".length();
            int squote = xmlproperties.indexOf("'",index1);
            int dquote = xmlproperties.indexOf("\"",index1);
            String quote = "";

            if(squote != -1 && (squote < dquote || dquote == -1)) {
                quote = "'";
            }
            else if(dquote != -1 && (dquote < squote || squote == -1)) {
                quote = "\"";
            }
            else {
                try {
                    throw new Exception("Invail xml code, missing parameters.");
                } catch (Exception e) {
                    new LLException(e);
                }
            }
            index1 = xmlproperties.indexOf(quote,index1)+quote.length();
            index2 = xmlproperties.indexOf(quote,index1);
            String value = xmlproperties.substring(index1,index2);

            properties.setProperty(key,value);
            index1 = index2+quote.length();
        }

        return properties;
    }
    public void print() {
        String xml = "";
        for(int i = 0;i <= tag.getChildren()-1;i++) {
            xml += printTag(tag.getChild(i),"");
        }
        xml =    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
                xml;
        System.out.println(xml);
    }
    private String printTag(Tag tag,String height) {
        String xml =    height+"<"+tag.getTag();

        Object[] keys = tag.getProperties().stringPropertyNames().toArray();
        String prop = " ";
        if(keys.length >= 5) {
            prop = "\n"+height+"    ";
        }

        for(int i = 0;i <= keys.length-1;i++) {
            xml += prop+keys[i]+"=\""+tag.getProperties().getProperty(keys[i].toString())+"\"";
        }
        if(tag.getBody() == null) {
            xml += "/>\n";
        }
        else {
            xml +=    LLString.replace(prop+">\n","    ","",1);
            for(int i = 0;i <= tag.getChildren()-1;i++) {
                xml += printTag(tag.getChild(i),height+"    ");
            }
            xml +=    height+"</"+tag.getTag()+">\n";
        }

        return xml;
    }
    //Implements;

}

并标记

package Lindholm.languages;

import java.util.Vector;

import Lindholm.com.LLProperty;

public class Tag {
    //STATIC variables;

    //Variables;
    private    String tag;
    private    String body;
    private    LLProperty properties;
    private    Vector<Tag> children = new Vector<Tag>();

    //Setup;

    //Constructor;
    public Tag(String name,String body) {
        tag = name;
        this.body = body;
    }
    //Set;
    public void setProperties(LLProperty properties) {
        this.properties = properties;
    }
    public void setChildren(Vector<Tag> children) {
        this.children = children;
    }

    //Get;
    public String getTag() {
        return tag;
    }
    public String getBody() {
        return body;
    }
    public LLProperty getProperties() {
        return properties;
    }
    public int getChildren() {
        return children.size();
    }
    public Tag getChild(int index) {
        return children.get(index);
    }
    public Tag getChildByTag(String tagname) {
        for(int i = 0;i <= getChildren()-1;i++) {
            if(getChild(i).getTag().equals(tagname)) {
                return getChild(i);
            }
        }
        return null;
    }
    public Tag getChildByTag(String tagname,int number) {
        for(int i = 0;i <= getChildren()-1;i++) {
            if(getChild(i).getTag().equals(tagname)) {
                if(number == 0) {
                    return getChild(i);
                }
                else {
                    number--;
                }
            }
        }
        return null;
    }

    //Add;
    public void addChild(Tag child) {
        children.add(child);
    }

    //Remove;

    //Do;

    //Other;

    //Implements;

}

现在我的问题是它非常慢,还有其他方法可以完成我想要的吗?或者可能是一种让它更快的方法?,也许我的代码很糟糕?

4

3 回答 3

10
  1. 在任何情况下都不要尝试编写自己的 XML 解析器。尽管表面上 XML 看起来很简单,但它实际上是一个非常复杂的标准,您会错过其中的一些内容。如果您也在控制 XML 创建,那么您可能会侥幸逃脱。但是,如果您是,为什么还要使用 XML?如果您不是,请准备好创建它的系统/库/供应商突然使用您自制的解析器无法处理的 XML 的高级功能。
  2. 有很多可用的开源解析器。现在甚至有一个内置到 JDK 中。您可以选择以 DOM 结构将整个文档读入内存,或者获取事件流 (SAX)。开源库还允许使用其他技术,例如 XML Pull。

看着:

于 2012-09-02T11:26:29.063 回答
1

您可以使用另一个 XML 解析器。我听说VTD-XML非常快。

于 2012-09-02T11:30:58.497 回答
1

在 java 中读取 XML 文件可以很容易地完成......

1.使用SAX解析器

2.使用DOM解析器

3.使用XML Pull Parsing

于 2012-09-02T13:08:00.390 回答