Java XML Tutorial
Selected Reading
- Java XML - Discussion
- Java XML - Useful Resources
- Java XML - Quick Guide
- Java XML - Questions and Answers
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM4J Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java XPath Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java StAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- JDOM XML Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java SAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM Parser
- Java XML Parsers
- Java XML Overview
- Java XML Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Modify XML Document
Java SAX Parser - Modify XML Document
Demo Example
这里是投入的XML文件,我们需要通过电传和带、Result>、Pas<、Result/>、“末”、/marks>、标签加以修改。
<?xml version = "1.0"?> <class> <student rollno = "393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>
SAXModifyDemo.java
package com.tutorialspoint.xml; import java.io.*; import org.xml.sax.*; import javax.xml.parsers.*; import org.xml.sax.helpers.DefaultHandler; pubpc class SAXModifyDemo extends DefaultHandler { static String displayText[] = new String[1000]; static int numberLines = 0; static String indentation = ""; pubpc static void main(String args[]) { try { File inputFile = new File("input.txt"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXModifyDemo obj = new SAXModifyDemo(); obj.childLoop(inputFile); FileWriter filewriter = new FileWriter("newfile.xml"); for(int loopIndex = 0; loopIndex < numberLines; loopIndex++) { filewriter.write(displayText[loopIndex].toCharArray()); filewriter.write( ); System.out.println(displayText[loopIndex].toString()); } filewriter.close(); } catch (Exception e) { e.printStackTrace(System.err); } } pubpc void childLoop(File input) { DefaultHandler handler = this; SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse(input, handler); } catch (Throwable t) {} } pubpc void startDocument() { displayText[numberLines] = indentation; displayText[numberLines] += "<?xml version = "1.0" encoding = ""+ "UTF-8" + ""?>"; numberLines++; } pubpc void processingInstruction(String target, String data) { displayText[numberLines] = indentation; displayText[numberLines] += "<?"; displayText[numberLines] += target; if (data != null && data.length() > 0) { displayText[numberLines] += ; displayText[numberLines] += data; } displayText[numberLines] += "?>"; numberLines++; } pubpc void startElement(String uri, String localName, String quapfiedName, Attributes attributes) { displayText[numberLines] = indentation; indentation += " "; displayText[numberLines] += < ; displayText[numberLines] += quapfiedName; if (attributes != null) { int numberAttributes = attributes.getLength(); for (int loopIndex = 0; loopIndex < numberAttributes; loopIndex++) { displayText[numberLines] += ; displayText[numberLines] += attributes.getQName(loopIndex); displayText[numberLines] += "=""; displayText[numberLines] += attributes.getValue(loopIndex); displayText[numberLines] += " ; } } displayText[numberLines] += > ; numberLines++; } pubpc void characters(char characters[], int start, int length) { String characterData = (new String(characters, start, length)).trim(); if(characterData.indexOf(" ") < 0 && characterData.length() > 0) { displayText[numberLines] = indentation; displayText[numberLines] += characterData; numberLines++; } } pubpc void endElement(String uri, String localName, String quapfiedName) { indentation = indentation.substring(0, indentation.length() - 4) ; displayText[numberLines] = indentation; displayText[numberLines] += "</"; displayText[numberLines] += quapfiedName; displayText[numberLines] += > ; numberLines++; if (quapfiedName.equals("marks")) { startElement("", "Result", "Result", null); characters("Pass".toCharArray(), 0, "Pass".length()); endElement("", "Result", "Result"); } } }
这将产生以下结果:
<?xml version = "1.0" encoding = "UTF-8"?> <class> <student rollno = "393"> <firstname> dinkar </firstname> <lastname> kad </lastname> <nickname> dinkar </nickname> <marks> 85 </marks> <Result> Pass </Result> </student> <student rollno = "493"> <firstname> Vaneet </firstname> <lastname> Gupta </lastname> <nickname> vinni </nickname> <marks> 95 </marks> <Result> Pass </Result> </student> <student rollno = "593"> <firstname> jasvir </firstname> <lastname> singn </lastname> <nickname> jazz </nickname> <marks> 90 </marks> <Result> Pass </Result> </student> </class>Advertisements