0

I am trying to delete some nodes from XML document but its not working here is the XML

<?xml version="1.0" encoding="UTF-8" ?> 
 <reg>
 <user>
  <Name>adik</Name> 
  <Email>[email protected]</Email> 
  <Picture>/storage/sdcard0/XLEZData/EZImage/20130425163759.PNG</Picture> 
  <LastEdited>7 Apr 2014 09:28:27</LastEdited> 
  </user>
 <user>
  <Name>adil</Name> 
  <Email>[email protected]</Email> 
  <Picture>/storage/sdcard0/DCIM/Camera/20140318_165923(0).jpg</Picture> 
  <LastEdited>7 Apr 2014 09:29:06</LastEdited> 
  </user>
</reg>

and here is the code

   private void DeleteRecord(String sEmail) {
        try {

            ////
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            Document document = dbf.newDocumentBuilder().parse(new File(
                    Environment.getExternalStorageDirectory()
                    + "/Reginfo/output/data.xml"));

            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            XPathExpression expression = xpath.compile("//reg/user[Email = '" + sEmail + "']");
            Log.v("expression", expression.toString());
            Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
            b13Node.getParentNode().removeChild(b13Node);
             try{
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            t.transform(new DOMSource(document), new StreamResult(System.out));
             }
             catch(Exception e){}
            ////
        }


         catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

    }

what i want is to delete whole user node with its children Email Name, LastEdited and Picture

1 Answer 1

1

Try xpath.compile("//reg/user[Email = '" + sEmail + "']");.

If the XML has namespaces then see http://www.edankert.com/defaultnamespaces.html#JAXP_XPathFactory on how to use the JAXP XPath API with namespaces to select nodes in a namespace.

Sign up to request clarification or add additional context in comments.

3 Comments

Does the real XML have any xmlns="..." on the root? What happens after you corrected the XPath expression construction, do you get an error respectively exception? You seem to catch exception but not doing anything in the catch clause, that makes it hard to tell what goes wrong. If the XPath does not select a node then b13Node.getParentNode() would throw an exception.
yes the real xml has xmlns and now logCat is printing all xml records on the screen which have <Email> . . .
i have updated the question please take a look now whole method is there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.