随笔-11  评论-5  文章-1  trackbacks-0

在这里包就不写进来了
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();//得到节点名
    //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 阅读(2658) 评论(1)  编辑  收藏 所属分类: Java web

评论:
# re: 结合w3c.dom和jdom 用Java语言实现对Xml的创建,插入、修改、查询的实例 2008-05-26 15:41 | 平静的尼洋河
也许这正是我需要的!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: