用java读取xml

时间:2012-09-02 11:15:17

标签: java xml

嘿我想用java读取和编写xml代码所以我google'd怎么做但我发现只有引擎可以读取真正简单的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;

}

现在我的问题是它增长缓慢,有没有其他方法可以实现我想要的?或者也许是一种让它更快的方法?,也许我的代码很糟糕?

3 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:1)

您可以使用其他XML解析器。我听说VTD-XML非常快。

答案 2 :(得分:1)

在java中读取XML文件可以非常轻松地完成....

1。使用SAX解析器

2。使用DOM解析器

3。使用XML Pull Parsing