Advertisement
Mlaszlo95

Untitled

May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package evo.teszt;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import javax.xml.transform.TransformerException;
  7.  
  8. import org.w3c.dom.*;
  9. import org.xml.sax.SAXException;
  10.  
  11. import evo.classes.Products;
  12.  
  13. public class DeleteXMLtag_test {
  14.  
  15.     public static void deleteTagFromTheXML(String productName) throws ParserConfigurationException, SAXException, IOException, TransformerException{
  16.         org.w3c.dom.Document doc = null;
  17.         Products.setFileLocation("proba.xml");
  18.         doc= Products.tryOpenTheFile();
  19.        
  20.         NodeList nodes = doc.getElementsByTagName("item");
  21.  
  22.         for (int i = 0; i < nodes.getLength(); i++) {
  23.           Element goods = (Element)nodes.item(i);
  24.  
  25.           Element name = (Element)goods.getElementsByTagName("name").item(0);
  26.           String dName = name.getTextContent();
  27.           System.out.println("Name: "+dName);
  28.          
  29.           if (dName.equals(productName)) {
  30.              System.out.println("Name: "+dName);
  31.              Node parent = goods.getParentNode();
  32.              parent.removeChild(goods);
  33.              parent.normalize();
  34.              Products.toString(doc);
  35.             }
  36.         }
  37.     }
  38.  
  39.    
  40.     public static void main(String[] args) {
  41.         try {
  42.             deleteTagFromTheXML("Ryzen 5 1600");
  43.         } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement