3

I'm developing an android application, and I would like to delete a small content in my XML file in my application's code. I would like to delete the content below:

<pma:structure_schemas>
    <pma:database name="test" collation="latin1_swedish_ci" charset="latin1">
        <pma:table name="agenda">
            CREATE TABLE `agenda` (
              `id` int(2) NOT NULL AUTO_INCREMENT,
              `title` varchar(60) NOT NULL,
              `description` varchar(1000) NOT NULL,
              `town` varchar(30) NOT NULL,
               PRIMARY KEY (`id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
        </pma:table>
    </pma:database>
</pma:structure_schemas>

Do you have any idea about how to do ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

You can use the following code:

import java.io.File;
import javax.xml.*;
import org.w3c.dom.*;

public class DeleteXmlNode {

    public static void deleteNode(String nodedetailsExpression) {//Use your expression here depending upon the node you wnt to delete

        try{
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expression = xpath.compile(nodedetailsExpression);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        Document document = documentBuilderFactory.newDocumentBuilder().parse(new File("test.xml"));
        Node node = (Node) expression.evaluate(document, XPathConstants.NODE);
        node.getParentNode().removeChild(node);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document), new StreamResult(System.out));
        }catch(Exception e){
        //Do your Exception handling over here.
    }

}

Here you need to pass the expression string of the node to be deleted.

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • i'm a newcomer in the android programation. I'll try to make some researches about what you posted. – user2184060 Apr 04 '13 at 13:12
  • Ok. Please try and let me know in case of any issue. – Shreyos Adikari Apr 04 '13 at 13:13
  • Ok. In my Sax program, to get my xml file, i use this line : url = new URL("internet adress/xml.xml"); Then, i would like to directly make disapear the XML content i showed to you in my post. I'm trying to understand what you sent, but i don't really know how to use it effectively. Do you have something that is easier for me ? Thank you a lot for your answer – user2184060 Apr 04 '13 at 13:22
  • i do think so but i hope that it won't be too complicated for me. – user2184060 Apr 04 '13 at 13:33
0

Take a look at this question. This is a very popular question, for next time google problems before asking here.

Community
  • 1
  • 1
ThaSaleni
  • 370
  • 1
  • 7
  • 21
  • 1
    i'm already using the sax parser to do this job. the problem is that certain tags block the using of the feed. – user2184060 Apr 04 '13 at 13:07
0

Here is the code to remove an element with VTD-XML. Notice it is a lot more terse and efficent than DOM

import com.ximpleware.*;
import java.io.*;

public class removeElement {
    public static void main(String s[]) throws VTDException,IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/ClOrdIDS/ClOrdID[@id='3']");
        int i=0;
        while((i=ap.evalXPath())!=-1){
            xm.remove();
        }
        xm.output("output.xml");
    }
}
vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30