Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:01:33+00:00 2026-06-11T02:01:33+00:00

Hey I’d like to read and write xml code with java so I google’d

  • 0

Hey I’d like to read and write xml code with java so I google’d how to do it but what i found was only engine’s who could read real easy xml code such as

<?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>

But I’d like to read more advance’d such as:

<?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>

So I went ahead and did my own classes for this,

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;

}

and tag

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;

}

Now my issue is that it’s increadably slow, is there any other way to accomplish what i want? or maybe a way to make it faster?, Maybe my code is bad?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:01:34+00:00Added an answer on June 11, 2026 at 2:01 am
    1. DON’T, under any circumstances, try and write your own XML parser. Although on the surface XML looks straightforward, it’s actually a very complex standard and you WILL miss bits of it. You might just get away with it if you are controlling the XML creation as well. But then if you are, why use XML at all? And if you’re not, be prepared for the system/library/vendor that is creating it to suddenly use an advanced feature of XML that your home-built parser doesn’t handle.
    2. There are plenty of open-source parsers available. There’s even one built in to the JDK now. You have the choice of reading the whole document into memory in a DOM structure, or getting an event-stream (SAX). Open source libraries also allow other technologies such as XML Pull.

    Look at:

    • Xerces: http://xerces.apache.org/ – a version of this is bundled with the JDK
    • JDOM: http://www.jdom.org/
    • Woodstox: http://woodstox.codehaus.org/
    • XMLPull: http://www.xmlpull.org/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, am trying to write to do type casting in java, but i
Hey I want to write a struct like the XNA Color struct or the
hey guys was hoping you could help me out.. The task seems simple but
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey guys I wanted to create a JScrollPane but it won't work... and I
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
Hey guys, sorry I'm kind of new to programming, but I had a small
Hey I was wondering if there was a specific query that I could enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.