Dom parsing modification and deletion method for Java knowledge accumulation-xml

Source: Internet
Author: User

Import Java.io.File;

Import java.io.IOException;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerConfigurationException;
Import javax.xml.transform.TransformerException;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.TransformerFactoryConfigurationError;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;

Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import Org.w3c.dom.Text;
Import org.xml.sax.SAXException;

public class Removenodetest {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Documentbuilderfactory factory= documentbuilderfactory.newinstance ();
Documentbuilder Builder;
try {
Builder = Factory.newdocumentbuilder ();
Document Doc=builder.parse (RemoveNodeTest.class.getResourceAsStream ("Student.xml"));
Element studentelenent=doc.createelement ("student");
Studentelenent.setattribute ("id", "jredu003");
Element name=doc.createelement ("name");
Text Namevalue=doc.createtextnode ("Xiaohuang");
Name.appendchild (Namevalue);
Element age=doc.createelement ("Age");
Text Agevalue=doc.createtextnode ("20");
Age.appendchild (Agevalue);
Studentelenent.appendchild (name);
Studentelenent.appendchild (age);
Element root = Doc.getdocumentelement ();
NodeList studentnodes = Root.getchildnodes ();
NodeList haha=root.getelementsbytagname ("name");
for (int i = 0; i < studentnodes.getlength (); i++) {
Node node = Studentnodes.item (i); Because the space is also considered an element, so to handle, do not output the space element
Node Node1=haha.item (i);
if (node.getnodetype () = = Node.element_node) {
String id = node.getattributes (). getNamedItem ("id")
. Getnodevalue ();
/*if (Id.equals ("003")) {
Doc.getdocumentelement (). ReplaceChild (studentelenent, node); Modify
Doc.getdocumentelement (). removechild (node); Delete
}*/
Delete the middle node in the same way
SYSTEM.OUT.PRINTLN (ID);
NodeList childs = Node.getchildnodes ();

for (int j = 0; J < Childs.getlength (); j + +) {
Node NodeE = Childs.item (j);
if (nodee.getnodetype () = = Node.element_node) {
String Aname = Nodee.getfirstchild (). Getnodevalue ();
if (Aname.equals ("Zhang San")) {
Nodee.settextcontent ("Hahah");
Nodee.getparentnode (). removechild (NodeE); Ways to delete a single intermediate node
}//methods for modifying intermediate values
System.out.println (Aname);
} else if (Nodee.getnodename (). Equals ("Age")) {
String Aage = Nodee.getfirstchild (). Getnodevalue ();
System.out.println (Aage);
}
}

}
}


Transformer transformer=transformerfactory.newinstance (). Newtransformer ();
Transformer.transform (New Domsource (DOC),
New Streamresult (New File ("C:\\a.xml"));


} catch (Parserconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Saxexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Transformerconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Transformerfactoryconfigurationerror e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Transformerexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

Dom parsing modification and deletion method for Java knowledge accumulation-xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.