隨筆-11  評(píng)論-5  文章-1  trackbacks-0

          在這里包就不寫(xiě)進(jìn)來(lái)了
          public class InputItem
          {
          ?public boolean creatInputFile(String fileName) throws FileNotFoundException, IOException
          ?{??
          ??ItemVB itemVB= new ItemVB();
          ??InputItem a = new InputItem();
          ??DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          ??
          ??DocumentBuilder builder=null;
          ??try
          ??{
          ???builder = factory.newDocumentBuilder();
          ??}
          ??catch(ParserConfigurationException e)
          ??{
          ???e.printStackTrace();
          ??}
          ??
          ??org.w3c.dom.Document doc = null;
          ??doc = builder.newDocument();
          ??org.w3c.dom.Element root = doc.createElement("item");
          ??doc.appendChild(root);
          ??
          ??DOMBuilder builders = new DOMBuilder();
          ??org.jdom.Document Doc = builders.build(doc);
          ??XMLOutputter XMLOut = new XMLOutputter();
          ??XMLOut.output(Doc,new FileOutputStream(fileName));
          ??
          ??return true;
          ?}
          ?
          ?public boolean insertItem(String fileName,ItemVB vb) throws FileNotFoundException, IOException
          ?{
          ??ItemVB itemVB= new ItemVB();
          ??InputItem a = new InputItem();?
          ??DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          ??DocumentBuilder builder=null;
          ??
          ??try
          ??{
          ???builder = factory.newDocumentBuilder();
          ??}
          ??catch (ParserConfigurationException e)
          ??{
          ???e.printStackTrace();
          ??}
          ??
          ??File f = new File(fileName);
          ??org.w3c.dom.Document doc=null;?
          ??
          ??try
          ??{
          ???doc = builder.parse(f);
          ??} catch (SAXException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??catch (IOException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??
          ??org.w3c.dom.Element root = doc.getDocumentElement();
          ??org.w3c.dom.Element eee = doc.createElement("id"+vb.getId());
          ??root.appendChild(eee);
          ??NodeList nameElements =? root.getElementsByTagName("id"+vb.getId());
          ??org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nameElements.item(0);
          ??System.out.println(nameElement);
          ??System.out.println(eee);
          ??
          ??if(eee==nameElement)
          ??{

          ????? org.w3c.dom.Element eee2 = doc.createElement("description");
          ???eee.appendChild(eee2);
          ???Text description = doc.createTextNode(vb.getDescription());
          ????? eee2.appendChild(description);
          ????? org.w3c.dom.Element eee3 = doc.createElement("value");
          ???eee.appendChild(eee3);
          ???Text value = doc.createTextNode(vb.getValue());
          ????? eee3.appendChild(value);
          ????? DOMBuilder builders = new DOMBuilder();
          ???org.jdom.Document Doc = builders.build(doc);
          ???XMLOutputter XMLOut1 = new XMLOutputter();
          ???XMLOut1.output(Doc,new FileOutputStream(fileName));
          ??}
          ??else
          ??{
          ???return false;
          ??}
          ??return true;
          ?}
          ?
          ?public boolean modifyItem(String fileName,ItemVB vb) throws FileNotFoundException, IOException
          ?{
          ??ItemVB itemVB= new ItemVB();
          ??InputItem a = new InputItem();
          ??DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          ??DocumentBuilder builder=null;
          ??XMLOutputter XMLOut = new XMLOutputter();
          ??File f = new File(fileName);
          ??
          ??try
          ??{
          ???builder = factory.newDocumentBuilder();
          ??}
          ??catch (ParserConfigurationException e)
          ??{
          ???e.printStackTrace();
          ??}
          ??
          ??org.w3c.dom.Document doc=null;?
          ??
          ??try
          ??{
          ???doc = builder.parse(f);
          ??} catch (SAXException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??catch (IOException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??
          ??DOMBuilder buuo = new DOMBuilder();
          ??Document doc1 = buuo.build(doc);
          ??
          ??org.w3c.dom.Element root = doc.getDocumentElement();
          ??NodeList nameElements =? root.getElementsByTagName("id"+vb.getId());
          ??????? org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nameElements.item(0);
          ??
          ??if(nameElement!=null)
          ??{?
          ???org.w3c.dom.Element ddd = (org.w3c.dom.Element)nameElements.item(0);
          ???NodeList nameDescriptions= ddd.getElementsByTagName("description");
          ???org.w3c.dom.Element nameDescription = (org.w3c.dom.Element) nameDescriptions.item(0);
          ???????? Text nameText1 = (Text) nameDescription.getFirstChild();
          ???nameText1.setData(vb.getDescription());
          ???NodeList nameValue = root.getElementsByTagName("value");
          ???org.w3c.dom.Element nameValues = (org.w3c.dom.Element) nameValue.item(0);
          ???Text nameText2 = (Text) nameValues.getFirstChild();
          ???nameText2.setData(vb.getValue());
          ???DOMBuilder builders = new DOMBuilder();
          ???org.jdom.Document Doc = builders.build(doc);
          ???XMLOutputter XMLOut1 = new XMLOutputter();
          ???XMLOut1.output(Doc,new FileOutputStream(fileName));
          ??}
          ??else
          ??{
          ???return false;
          ??}
          ??return true;?
          ?}
          ?
          ?public ItemVB searchItem(String id,String fileName)
          ?{
          ??ItemVB itemVB = new ItemVB();
          ??InputItem a = new InputItem();
          ??DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          ??DocumentBuilder builder =null;
          ??XMLOutputter XMLOut = new XMLOutputter();
          ??File f = new File(fileName);
          ??
          ??try
          ??{
          ???builder = factory.newDocumentBuilder();
          ??}
          ??catch (ParserConfigurationException e)
          ??{
          ???e.printStackTrace();
          ??}
          ??
          ??org.w3c.dom.Document doc=null;?
          ??
          ??try
          ??{
          ???doc = builder.parse(f);
          ??} catch (SAXException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??catch (IOException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??
          ??DOMBuilder buuo = new DOMBuilder();
          ??Document doc1 = buuo.build(doc);
          ??
          ??org.w3c.dom.Element root = doc.getDocumentElement();
          ??NodeList nameElements =? root.getElementsByTagName("id"+id);
          ??org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nameElements.item(0);
          ??
          ??if(nameElement!=null)
          ??{?
          ???org.w3c.dom.Element ddd = (org.w3c.dom.Element)nameElements.item(0);
          ???NodeList nameDescriptions= ddd.getElementsByTagName("description");
          ???org.w3c.dom.Element nameDescription = (org.w3c.dom.Element) nameDescriptions.item(0);
          ???Text nameText1 = (Text) nameDescription.getFirstChild();
          ???itemVB.setDescription(nameText1.getData());
          ???NodeList nameValue = root.getElementsByTagName("value");
          ???org.w3c.dom.Element nameValues = (org.w3c.dom.Element) nameValue.item(0);
          ???Text nameText2 = (Text) nameValues.getFirstChild();
          ???itemVB.setValue(nameText2.getData());
          ???itemVB.setId(id);
          ??}
          ??else
          ??{
          ???return null;
          ??}
          ??return itemVB;
          ?}
          ?
          ?public ArrayList creatIndexList(String indexFile,String itemFile)
          ??? {

          ??ArrayList a= new ArrayList();
          ??DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          ??DocumentBuilder builder=null;
          ??File f = new File(indexFile);
          ??File f1 = new File(itemFile);
          ??try
          ??{
          ???builder = factory.newDocumentBuilder();
          ??}
          ??catch (ParserConfigurationException e)
          ??{
          ???e.printStackTrace();
          ??}
          ??
          ??org.w3c.dom.Document doc = null;
          ??org.w3c.dom.Document doc1 = null;
          ??
          ??try
          ??{
          ???doc = builder.parse(f);
          ???doc1 = builder.parse(f1);
          ??}
          ??catch (SAXException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??catch (IOException e1)
          ??{
          ???e1.printStackTrace();
          ??}
          ??//a.add(vv.getIndex());
          ??//System.out.println(a.size());
          ??org.w3c.dom.Element root = doc.getDocumentElement();
          ??
          ??NodeList children? = (NodeList) root.getChildNodes();//children = [JobManage: null]
          ??
          ??for(int i=0;i<children.getLength();i++)
          ??{???
          ???if(children.item(i).getNodeType()==Node.ELEMENT_NODE)
          ???{
          ????String m = children.item(i).getNodeName();//得到節(jié)點(diǎn)名
          ????//System.out.println("m="+m);
          ????NodeList ids = root.getElementsByTagName(m);
          ????org.w3c.dom.Element ts = (org.w3c.dom.Element) ids.item(0);
          ????
          ????NodeList fast = ts.getElementsByTagName("id");
          ????org.w3c.dom.Element fast1 = (org.w3c.dom.Element) fast.item(0);
          ?????? Text fast2 = (Text)fast1.getFirstChild();
          ????String fast3 = fast2.substringData(0,10);
          ????//System.out.println("fast3="+fast3);
          ????org.w3c.dom.Element root1 = doc1.getDocumentElement();
          ????NodeList MMU = (NodeList)root1.getChildNodes();
          ??????????????? for(int j=0;j<MMU.getLength();j++)
          ??????????????? {
          ??????????????? ?if(MMU.item(j).getNodeType()==Node.ELEMENT_NODE)
          ??????????????? ?{?
          ???????????????? ?String n = MMU.item(j).getNodeName();
          ???????????????? ?NodeList nameElement1s =? root1.getElementsByTagName(n);
          ???????????????? ?org.w3c.dom.Element nameElement1 = (org.w3c.dom.Element) nameElement1s.item(0);
          ???????????????? ?String str2 = nameElement1.getNodeName();
          ???????????????? ?
          ???????????????? ?NodeList descriptions = nameElement1.getElementsByTagName("description");
          ????????????????????
          ?????????? ?org.w3c.dom.Element description = (org.w3c.dom.Element) descriptions.item(0);
          ?????????? ?
          ?????????? ?Text namedescription = (Text) description.getFirstChild();
          ?????????? ?//System.out.println("222222222"+namedescription);
          ?????????? ?NodeList values = nameElement1.getElementsByTagName("value");
          ?????????? ?org.w3c.dom.Element value = (org.w3c.dom.Element) values.item(0);
          ?????????? ?Text namevalue = (Text) value.getFirstChild();
          ?????????? ?//System.out.println(namevalue);
          ?????????? ?String namedescription1 = namedescription.toString();
          ?????????? ?String namedescription2 = namedescription1.substring(namedescription1.indexOf(":",1)+2,namedescription1.indexOf("]"));
          ?????????? ?//System.out.println("321"+namedescription2);
          ?????????? ?String namevalue1 = namevalue.toString();
          ?????????? ?String namevalue2 = namevalue1.substring(namevalue1.indexOf(":",1)+2,namevalue1.indexOf("]"));
          ??????????? if(fast3.equals(str2))
          ??????????? {??
          ?
          ??????????? ?IndexVB qh = new IndexVB();
          ??????????? ?qh.setIndex(m);
          ??????????? ?qh.setDescription(namedescription2);
          ??????????? ?qh.setValue(namevalue2);
          ????????????????????????
          ???????????????????????? //System.out.println("vv"+vv.getIndex()+vv.getDescription()+vv.getValue());
          ??????????? ?a.add(qh);
          ??????????? }
          ?????????? }
          ??????????????? }
          ???}
          ??}
          ??return a;
          ?}

          ?/*public static void main(String[] s) throws FileNotFoundException, IOException, JDOMException
          ?{
          ??InputItem bb = new InputItem();
          ??String indexFile = "C:\\index.xml";
          ??String itemFile = "C:\\Item.xml";
          ??ArrayList a;
          ??//bb.creatIndexList(indexFile,itemFile,vb,vv);
          ??
          ??a = bb.creatIndexList("C:\\index.xml","C:\\Item.xml");
          ??for(int i=0;i<a.size();i++)
          ??{
          ???System.out.println(((IndexVB)a.get(i)).getIndex());
          ??}
          ?}*/
          }

          ?

          posted on 2006-07-28 09:26 Crespo 閱讀(2665) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): Java web

          評(píng)論:
          # re: 結(jié)合w3c.dom和jdom 用Java語(yǔ)言實(shí)現(xiàn)對(duì)Xml的創(chuàng)建,插入、修改、查詢的實(shí)例 2008-05-26 15:41 | 平靜的尼洋河
          也許這正是我需要的!  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 嘉定区| 洪泽县| 广汉市| 离岛区| 潍坊市| 富宁县| 阿拉善右旗| 日土县| 资中县| 张北县| 瑞丽市| 广平县| 临湘市| 德钦县| 弥渡县| 望城县| 阳朔县| 桂平市| 孟津县| 嘉兴市| 万盛区| 鹤峰县| 兰溪市| 策勒县| 宁陕县| 万源市| 邯郸县| 德令哈市| 略阳县| 筠连县| 饶阳县| 景德镇市| 如东县| 玉溪市| 晋城| 四子王旗| 合肥市| 仙桃市| 屏南县| 徐闻县| 福海县|