如何使用SAX(Java)在XML文件中写入最后一个值

时间:2019-05-22 09:34:32

标签: java xml sax

我希望能够读取我的XML文件(现在正在做),并在可能的情况下使用SAX添加值。但是仅在我的XML末尾。 这是我的XML的样子:

    <symbology>

    <symbol>
    <hierarchy>1.X</hierarchy>
    <symbolId>SymbolId_1</symbolId>
    <description>MyIcon</description>
    </symbol>

    <symbol>
    <hierarchy>2.X</hierarchy>
    <symbolId>SymbolId_1</symbolId>
    <description>MyIcon</description>
    </symbol>

<!-- Add value here -->
    <symbology>

我正在使用Sax读取我的XML文件。现在这是我的代码:

    public void addCustomIcon(String symbol){
    if(_handleCustomSymbols){
        String custoName = "/com/test/program/base/resources/symbology/"+_customSymbolsFile;
        SAXParser parserSAX = null;
        SAXParserFactory    factory = SAXParserFactory.newInstance();
        try (InputStream    xmlCustoIn = SymbologyImpl.class.getResourceAsStream(custoName) ) {
            if (xmlCustoIn != null) {
                parserSAX = null;
                parserSAX = factory.newSAXParser();

                DefaultHandler defaultHandler = new DefaultHandler(){

                     boolean bHierarchy = false; 
                     boolean bSymbolid = false; 
                     boolean bDescription = false; 

                     public void startDocument(){
                         System.out.println("start document");
                     }

                     public void startElement(String uri, String localName, String qName, Attributes attributes){

                         System.out.println("start element : " + qName);
                         if(qName.equalsIgnoreCase("hierarchy")){
                             bHierarchy = true; 
                         }


                         if(qName.equalsIgnoreCase("symbolid")){
                             bSymbolid = true; 
                         }


                         if(qName.equalsIgnoreCase("description")){
                             bDescription = true; 
                         }
                     }

                     public void endElement(String uri, String localName, String qName){
                         System.out.println("EndElement : " + qName);
                     }

                     public void characters(char ch[], int start, int length){

                         if(bHierarchy){
                             System.out.println("Hirarchy : " + new String(ch, start, length));
                             bHierarchy = false;
                         }

                         if(bSymbolid){
                             System.out.println("SymbolId : " + new String(ch, start, length));
                             bSymbolid = false;
                         }

                         if(bDescription){
                             System.out.println("Description : " + new String(ch, start, length));
                             bDescription = false;
                         }
                     }

                     public void endDocument(){
                         System.out.println("end document");
                     }
                 };


                parserSAX.parse(xmlCustoIn, defaultHandler);
            }
        } catch (SAXException | IOException | ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

但是,我不知道如何在此文件的末尾为我的XML文件添加额外的值...谢谢!

我尝试使用DOM,并且文档始终为null ... 因此,我的DOM代码:

    public void readXMLCustoFile(String name){



    try {
        InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(resourceAsStream);

        Node firstChild = dom.getFirstChild();
        System.out.println("firstChild : " + firstChild.getNodeName());
    } catch (SAXException | IOException | ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

我的XML在我的项目中的位置: enter image description here

readXMLCustoFile中的名称为:字符串custoName =“ /com/test/program/base/resources/symbology/custoSymbol.xml”;

XML看起来像这样:

    <symbology>

<symbol>
<hierarchy>1.X</hierarchy>
<symbolId>SymbolId_1</symbolId>
<description>MyIcon</description>
</symbol>

<symbol>
<hierarchy>2.X</hierarchy>
<symbolId>SymbolId_1</symbolId>
<description>MyIcon</description>
</symbol>

    

我只想添加这样的新值:

<symbol>
        <hierarchy>X.X</hierarchy>
        <symbolId>NewSymbolId_1</symbolId>
        <description>NewDescriptionIcon</description>
</symbol>

祝你有美好的一天!

0 个答案:

没有答案