将数据附加到XML在java中没有提供协议错误

时间:2014-03-12 10:24:15

标签: java xml xml-parsing

我正在将内容写入xml文件。内容涉及人员,组织和名称。地点 。现在,如果文件不存在,则会创建一个新文件并将内容写入其中。此过程非常正常。

但是,如果文件已经存在,并且我尝试向其添加内容,我会得到:没有协议错误。

我想问题是将文件附加到已创建的XML文件中。经过一些搜索,我发现问题出在DocumentBuilder.parse()上。所以我也尝试使用:

Document document = documentBuilder.parse(new InputSource(new StringReader(filename +“。xml”))); 但仍然存在错误。

任何人都可以帮助我吗? 这是我的代码:

public static void xml_writer(ArrayList<String> per_names,ArrayList<String> loc_names,ArrayList<String> org_names,String filename,Logger L,Logger L1)
{   File file = new File(filename+".xml");
    // if file doesnt exists, then create it        
    if (!file.exists()) 
    {
        try { 
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();         
            Element rootElement = doc.createElement("Nouns");
            doc.appendChild(rootElement);
            Element staff = doc.createElement("Per_Names");
            for (int i=0;i<per_names.size();i++)
            {                       
                rootElement.appendChild(staff);  
                Element firstname = doc.createElement("Name");
                firstname.appendChild(doc.createTextNode(per_names.get(i)));
                staff.appendChild(firstname);    
            }       
            staff = doc.createElement("Org_Names");
            for (int i=0;i<org_names.size();i++)
            {   
                rootElement.appendChild(staff);  
                Element firstname = doc.createElement("Name");
                firstname.appendChild(doc.createTextNode(org_names.get(i)));
                staff.appendChild(firstname);    
            }
            staff = doc.createElement("Loc_Names");
            for (int i=0;i<loc_names.size();i++)
            {   
                rootElement.appendChild(staff);  
                Element firstname = doc.createElement("Name");
                firstname.appendChild(doc.createTextNode(loc_names.get(i)));
                staff.appendChild(firstname);    
            }
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD,"xml"); 
            StreamResult result = new StreamResult(file);
            transformer.transform(source, result);     
            //System.out.println("File saved!");
            L.info("writing to xml file successfull: "+filename);
        } 
        catch (ParserConfigurationException pce) {
            L.info("Error : writing to xml file unsuccessfull :"+filename);
            L.info("Error : "+pce.getLocalizedMessage());
            L1.info("Error : writing to xml file unsuccessfull :"+filename);
            L1.info("Error : "+pce.getLocalizedMessage());
            pce.printStackTrace();
            } 
        catch (TransformerException tfe) {
            L.info("Error : writing to xml file unsuccessfull :"+filename);
            L.info("Error :"+tfe.getLocalizedMessage());
            L1.info("Error : writing to xml file unsuccessfull :"+filename);
            L1.info("Error :"+tfe.getLocalizedMessage());
            tfe.printStackTrace();
        }
    }

    else
    {   try
        {
        System.out.println("filealready exists");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            //Document document = documentBuilder.parse( new InputSource(new StringReader(filename+".xml")));//new InputSource((filename+".xml"))); 
            Document document = documentBuilder.parse(filename+".xml");
        Element root = document.getDocumentElement();
        // Root Element   
        Element server = document.createElement("Per_Names");
        for (int i=0;i<per_names.size();i++)
        {                   
            Element name = document.createElement("Name");
            name.appendChild(document.createTextNode(per_names.get(i)));
            server.appendChild(name);       
            root.appendChild(server);
        }
        server = document.createElement("Org_Names");
        for (int i=0;i<org_names.size();i++)
        {                   
            Element name = document.createElement("Name");
            name.appendChild(document.createTextNode(org_names.get(i)));
            server.appendChild(name);       
            root.appendChild(server);
        }
        server = document.createElement("Loc_Names");
        for (int i=0;i<loc_names.size();i++)
        {                   
            Element name = document.createElement("Name");
            name.appendChild(document.createTextNode(loc_names.get(i)));
            server.appendChild(name);       
            root.appendChild(server);
        }

        DOMSource source = new DOMSource(document);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);
        L.info("writing output to xml successfull :"+filename);
        } 
    catch (ParserConfigurationException pce) {
        L.info("Error : writing to xml file unsuccessfull :"+filename);
        L.info("Error : "+pce.getLocalizedMessage());
        L1.info("Error : writing to xml file unsuccessfull :"+filename);
        L1.info("Error : "+pce.getLocalizedMessage());
        pce.printStackTrace();
        } 
    catch (TransformerException tfe) {
        L.info("Error : writing to xml file unsuccessfull :"+filename);
        L.info("Error : "+tfe.getLocalizedMessage());
        L1.info("Error : writing to xml file unsuccessfull :"+filename);
        L1.info("Error : "+tfe.getLocalizedMessage());
        tfe.printStackTrace();
    } catch (SAXException e) {
        L.info("Error : writing to xml file unsuccessfull :"+filename);
        L.info("Error : "+e.getLocalizedMessage());
        L1.info("Error : writing to xml file unsuccessfull :"+filename);
        L1.info("Error : "+e.getLocalizedMessage());
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        L.info("Error : writing to xml file unsuccessfull :"+filename);
        L.info("Error : "+e.getLocalizedMessage());
        L1.info("Error : writing to xml file unsuccessfull :"+filename);
        L1.info("Error : "+e.getLocalizedMessage());
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

}

0 个答案:

没有答案