Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See Root 321, rivers with one.
Meekness Conscience Lord, willing to do without regrets to the most bitter. Reading exercise strong body and mind, Prudential advised and the line and cherish.
jdk:1.8
xml:1.0
Os:windows7 x64
Ide:myeclipse
The original XML
<?xml version= "1.0" encoding= "UTF-8"? ><shopping><book><name id= "0001" > Armature </name>< Price discount= "originalprice=" >90</price><count>3</count><comment> default high praise </ Comment></book><book><name id= "0002" > Q </name><price discount= "originalPrice=" ">180</price><count>5</count><comment> Default rated </comment></book><book ><name id= "0003" > Typhoid Miscellaneous diseases </name><price discount= "originalprice=" >270</price>< count>7</count><comment> Default Praise </comment></book></shopping>
Code:
Package Jizuiku.xml_e;import Javax.xml.parsers.documentbuilder;import javax.xml.parsers.DocumentBuilderFactory; Import Javax.xml.transform.transformer;import Javax.xml.transform.transformerfactory;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;/** * JAXP Create a label with content without attributes under a tag * * @author to the hardest * @version V17.10.10 */public class Jaxpdemo {public static void main (string[] args) Throws exception{//Create parser factory documentbuilderfactory builderfactory = Documentbuilderfactory.newinstance ();// Create parser Documentbuilder builder = Builderfactory.newdocumentbuilder ();//parse XML returns document//URI as relative path string uri = "src\\ Jizuiku\\xml_e\\shopping.xml ";//document, import org.w3c.dom.document;document document = Builder.parse (URI);// Get all the book elements nodelist booknodelist = document.getElementsByTagName ("book");//Under the First Book tab, create a <sales>1000</Sales>node Firstbooknode = booknodelist.item (0);//Create label element Salesnode = document.createelement ("Sales"); Text Salesnodetext = document.createTextNode ("1000");//Add text to the label Salesnode.appendchild (salesnodetext);// Add the label to the label Firstbooknode.appendchild (Salesnode);//write-back operation, feel very complex ah transformerfactory transformerfactory = Transformerfactory.newinstance (); Transformer Transformer = Transformerfactory.newtransformer (); Transformer.transform (new Domsource (document), new Streamresult (URI));}}
The modified XML
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"? ><shopping><book><name id= "0001" > Armatures </name><price discount= "Originalprice=" >90</price><count>3</count>< comment> default Praise </comment><sales>1000</sales></book><book><name id= "0002" > Vegetarian Q </name><price discount= "Originalprice=" >180</price><count>5</count>< comment> default Praise </comment></book><book><name id= "0003" > Typhoid Miscellaneous Diseases </name><price Discount= "originalprice=" >270</price><count>7</count><comment> default high praise </ Comment></book></shopping>
Although the modification was successful, the format of the XML file changed
XML is good and worth learning.
Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.
JAXP Dom-appendchild Create a label with content without attributes under one tab