Insert new element to an XML file using SAX Filter

前端 未结 3 593
逝去的感伤
逝去的感伤 2021-01-20 01:22

I have an XMl file that looks like:



  
    2
    

        
3条回答
  •  遥遥无期
    2021-01-20 01:38

    Correct me if i'm wrong but i think XMLReader and XMLFilter are not really supposed to change a document. I can provide a different approach which with you can change the content of your document too:

    public class ExtXMLConfig {
    private JAXBContext context;
    private Marshaller m;
    private Unmarshaller um;
    private Schema schema = null;
    
    /**
     * Creates an ExtXMLConfig-object, which uses rootClass as object to parse
     * and save XML-files.
     * 
     * @param rootClass
     *            the class use create/parse xml-files from
     * @throws JAXBException
     */
    public ExtXMLConfig(Class rootClass) throws JAXBException {
        context = JAXBContext.newInstance(rootClass);
    
        init();
    }
    
    /**
     * Creates an ExtXMLConfig, which uses a classPath like javax.xml.bin to use
     * all classes in that path to parse and write xml-files
     * 
     * @param classPath
     *            the class path containing all needed java-objects
     * @throws JAXBException
     */
    public ExtXMLConfig(String classPath) throws JAXBException {
        context = JAXBContext.newInstance(classPath);
    
        init();
    }
    
    /**
     * Parses a xml-file into a JavaObject.
     * 
     * @param file
     *            path to the xml-file
     * @return a java-Object
     */
    public Object load(String file) {
        return load(new File(file));
    }
    
    /**
     * Parses a xml-file into a JavaObject.
     * 
     * @param xml
     *            File-object representing the xml-file
     * @return a java-Object
     */
    public Object load(File xml) {
        um.setSchema(schema);
    
        if (xml.exists() && xml.isFile()) {
            try {
                return um.unmarshal(xml);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("Failed to open file: " + xml.getAbsolutePath());
        }
    
        return null;
    }
    
    /**
     * Saves a object into a xml-file.
     * 
     * @param xml
     *            the object to save
     * @param file
     *            path to the file to save to
     */
    public void save(Object xml, String file) {
        save(xml, new File(file));
    }
    
    /**
     * Saves a object into a xml-file.
     * 
     * @param xml
     *            the object to save
     * @param file
     *            File-object representing the file to save to
     */
    public void save(Object xml, File file) {
        if (xml != null) {
            m.setSchema(schema);
    
            if (!file.isDirectory()) {
                try {
                    if (!file.exists()) {
                        file.createNewFile();
                    }
    
                    m.marshal(xml, file);
                } catch (JAXBException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * Returns a formatted string representation of a xml-file given as a
     * java-Object.
     * 
     * @param xml
     *            the java-object to parse the xml from.
     * @return a formatted string representation of the given object
     */
    public String toString(Object xml) {
        StringWriter out = new StringWriter();
        try {
            m.setSchema(schema);
            m.marshal(xml, out);
    
            return out.toString();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return null;
    }
    
    private void init() throws JAXBException {
        m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    
        um = context.createUnmarshaller();
    }
    

    Using this class to parse your xml-Files you would only need a Class like this:

    @XmlRootElement // used to parse this class as xml-Root
    public class Game {
       private Move moves;
    
       public Game() {};
    
       public void setMove(Move moves) {
          this.moves = moves;
       }
    
       public Moves getMoves() {
          return this.moves;
       }
    }
    

    with Move being an instance of another class which has the fields you need and also has a annotation for XmlRootElement.

    I hope this helps.

提交回复
热议问题