⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlchanger.java

📁 《Java案例开发》源代码( 考勤系统的完整实现!) 包括数据库 网络通讯&uml
💻 JAVA
字号:
/* * XMLChanger.java * * Created on 2004年1月25日, 下午2:15 */package romulus;import javax.xml.parsers.*;import org.xml.sax.*;import java.io.*;import org.w3c.dom.*;/** * * @author  S */public class XMLChanger {        /** Creates a new instance of XMLChanger */    private XMLChanger() {    }        static void GetChoiceItem(Choice cho, Element ele_cho) throws RomulusException{        ChoiceItem icho = null;        for (Node child = ele_cho.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("ChoiceItem")){                Element ele_icho = (Element)child;                boolean iscorr = false;                for (Node childcorr = ele_cho.getFirstChild(); childcorr != null; childcorr = childcorr.getNextSibling()) {                    if(childcorr instanceof Element && childcorr.getNodeName().equalsIgnoreCase("CorrectChoice")){                        if(ele_icho.getAttribute("ident").equalsIgnoreCase(((Element)childcorr).getAttribute("ident"))){                            iscorr = true;                        }                    }                }                icho = new ChoiceItem(ele_icho.getAttribute("ident"), ele_icho.getAttribute("label"), iscorr);                icho.setContent(GetContent(ele_icho));                                cho.addChoiceItem(icho);            }        }    }        static void GetContentItem(Content con, Element ele_con) throws RomulusException{        ContentItem icon = null;        for (Node child = ele_con.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("TextContent")){                Element ele_txt = (Element)child;                String txt = null;                try{                    txt = ele_txt.getFirstChild().getNodeValue();                }                catch(DOMException e){                    e.printStackTrace();                }                icon = new TextContent(ele_txt.getAttribute("ident"), txt, ele_txt.getAttribute("isem").equalsIgnoreCase("yes")?true:false);                                con.Add(icon);            }        }    }        static Content GetContent(Element ele) throws RomulusException{        Content ret = null;        for (Node child = ele.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("Content")){                Element ele_con = (Element)child;                ret = new Content(ele_con.getAttribute("ident"), ele_con.getAttribute("label"));                                GetContentItem(ret, ele_con);                                return ret;            }        }        return ret;    }        static void GetObjective(Question que, Element ele_que) throws RomulusException{        Objective obj = null;        for (Node child = ele_que.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("Objective")){                Element ele_obj = (Element)child;                obj = new Objective(ele_obj.getAttribute("ident"), View.getView(ele_obj.getAttribute("can_view")));                                obj.setContent(GetContent(ele_obj));                                que.addObjective(obj);            }        }    }        static void GetFeedback(Question que, Element ele_que) throws RomulusException{        Feedback feb = null;        for (Node child = ele_que.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("Feedback")){                Element ele_feb = (Element)child;                feb = new Feedback(ele_feb.getAttribute("ident"), View.getView(ele_feb.getAttribute("can_view")), ele_feb.getAttribute("title"));                                    feb.setContent(GetContent(ele_feb));                                que.addFeedback(feb);            }        }    }        static void GetQuestionItem(Question que, Element ele_que) throws RomulusException{        for (Node child = ele_que.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("FIB")){                Element ele_fib = (Element)child;                                String corr = null;                for(Node child_corr = ele_fib.getFirstChild(); child_corr != null; child_corr = child_corr.getNextSibling()){                    if(child_corr instanceof Element && child_corr.getNodeName().equalsIgnoreCase("CorrectAnswer")){                        try{                            corr = child_corr.getFirstChild().getNodeValue();                        }                        catch(DOMException e){                            e.printStackTrace();                        }                    }                }                                FIB fib;                                String str_max = ele_fib.getAttribute("maxchars");                if(str_max == null || str_max.length() == 0){                    fib = new FIB(ele_fib.getAttribute("ident"), 999, corr);                }                else{                    fib = new FIB(ele_fib.getAttribute("ident"), Integer.parseInt(str_max), corr);                }                                fib.setContent(GetContent(ele_fib));                                que.setQuestionItem(fib);                break;            }            else if(child instanceof Element && child.getNodeName().equalsIgnoreCase("Choice")){                Element ele_cho = (Element)child;                                Choice cho;                String rec = ele_cho.getAttribute("rcardinality");                if(rec == null || rec.length() == 0){                    cho = new Choice(ele_cho.getAttribute("ident"), 1, ele_cho.getAttribute("shuffle").equalsIgnoreCase("yes")?true:false);                }                else{                    cho = new Choice(ele_cho.getAttribute("ident"), Integer.parseInt(rec), ele_cho.getAttribute("shuffle").equalsIgnoreCase("yes")?true:false);                }                cho.setContent(GetContent(ele_cho));                                GetChoiceItem(cho, ele_cho);                               que.setQuestionItem(cho);            }        }    }        static void GetQuestion(Test t, Element ele_t) throws RomulusException{        Question que = null;        for (Node child = ele_t.getFirstChild(); child != null; child = child.getNextSibling()) {            if(child instanceof Element && child.getNodeName().equalsIgnoreCase("Question")){                Element ele_que = (Element)child;                String str_timing = ele_que.getAttribute("timing");                if(str_timing == null || str_timing.length() == 0){                    que = new Question(ele_que.getAttribute("ident"), 0, ele_que.getAttribute("title"));                }                else{                    que = new Question(ele_que.getAttribute("ident"), Long.parseLong(ele_que.getAttribute("timing")), ele_que.getAttribute("title"));                }                                GetObjective(que, ele_que);                                GetFeedback(que, ele_que);                                GetQuestionItem(que, ele_que);                                t.add(que);            }        }    }        public static Test GetTestFromXML(File xmlfile) throws RomulusException{        if(!xmlfile.isFile()){            throw new RomulusException("Not a file");        }                try{            DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();            fac.setValidating(true);            DocumentBuilder builder = fac.newDocumentBuilder();            builder.setErrorHandler(new org.xml.sax.ErrorHandler() {                // ignore fatal errors (an exception is guaranteed)                public void fatalError(SAXParseException exception)                throws SAXException {                }                                // treat validation errors as fatal                public void error(SAXParseException e)                throws SAXParseException {                    throw e;                }                                // dump warnings too                public void warning(SAXParseException err)                throws SAXParseException {                    System.out.println("** Warning"                    + ", line " + err.getLineNumber()                    + ", uri " + err.getSystemId());                    System.out.println("   " + err.getMessage());                }            });            Document doc = builder.parse(xmlfile);            Element root = doc.getDocumentElement();            String str_timing = root.getAttribute("timing");            Test ret;            if(str_timing == null || str_timing.length() == 0){                ret = new Test(root.getAttribute("ident"), 0, root.getAttribute("title"));            }            else{                ret = new Test(root.getAttribute("ident"), Long.parseLong(root.getAttribute("timing")), root.getAttribute("title"));            }                        GetQuestion(ret, root);                        return ret;        }        catch(Exception e){            e.printStackTrace();            throw new RomulusException("XML Error");        }    }        public static void main(String args[]){        try{            Test ret = XMLChanger.GetTestFromXML(new File("F:/My_Java_Book/BookWork/XML/Test_EG1.xml"));                        System.out.println("Finish");        }        catch(Exception e){            e.printStackTrace();        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -