Java/XML/CDATA — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:11, 1 июня 2010
Содержание
- 1 Append and insert data to CharacterData
- 2 Converting CDATA Nodes into Text Nodes While Parsing an XML File
- 3 Delete data from CharacterData
- 4 Escaping CDATA sections.
- 5 Get character data (CDATA) from xml document
- 6 Get substring from CharacterData
- 7 Replace Data in CharacterData
- 8 Set Data to Character Data
- 9 Strip CDATA
Append and insert data to CharacterData
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.rument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
CDATASection cdataNode = doc.createCDATASection("");
Comment commentNode = doc.createComment("");
Text textNode = doc.createTextNode("");
// All three types of nodes implement the CharacterData interface
CharacterData cdata = cdataNode;
cdata = commentNode;
cdata = textNode;
// data
int offset = 5;
cdata.insertData(offset, "a ");
cdata.appendData(" b");
}
}
Converting CDATA Nodes into Text Nodes While Parsing an XML File
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Main {
public static void main(String[] argv) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));
// doc will not contain any CDATA nodes
}
}
Delete data from CharacterData
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.rument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
CDATASection cdataNode = doc.createCDATASection("");
Comment commentNode = doc.createComment("");
Text textNode = doc.createTextNode("");
// All three types of nodes implement the CharacterData interface
CharacterData cdata = cdataNode;
cdata = commentNode;
cdata = textNode;
// Delete text
int offset = 0;
int len = 5;
cdata.deleteData(offset, len);
}
}
Escaping CDATA sections.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* This class is for escaping CDATA sections.
*
* @author
* @version $Id: Escape.java 463298 2006-10-12 16:10:32Z henning $
*/
public class Escape {
/**
*
*/
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
/**
* Empty constructor
*/
public Escape() {
// left blank on purpose
}
/**
* Do the escaping.
*
* @param st
* @return The escaped text.
*/
public static final String getText(String st) {
StringBuffer buff = new StringBuffer();
char[] block = st.toCharArray();
String stEntity = null;
int i, last;
for (i = 0, last = 0; i < block.length; i++) {
switch (block[i]) {
case "<":
stEntity = "<";
break;
case ">":
stEntity = ">";
break;
case "&":
stEntity = "&";
break;
case """:
stEntity = """;
break;
case "\n":
stEntity = LINE_SEPARATOR;
break;
default:
/* no-op */
break;
}
if (stEntity != null) {
buff.append(block, last, i - last);
buff.append(stEntity);
stEntity = null;
last = i + 1;
}
}
if (last < block.length) {
buff.append(block, last, i - last);
}
return buff.toString();
}
}
Get character data (CDATA) from xml document
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("data.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("topic");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("title");
Element line = (Element) title.item(0);
System.out.println("Title: " + getCharacterDataFromElement(line));
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
}
Get substring from CharacterData
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.rument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
CDATASection cdataNode = doc.createCDATASection("");
Comment commentNode = doc.createComment("");
Text textNode = doc.createTextNode("");
// All three types of nodes implement the CharacterData interface
CharacterData cdata = cdataNode;
cdata = commentNode;
cdata = textNode;
cdata.setData("some data");
int len = cdata.getLength();
int offset = 5;
len = 4;
String s = cdata.substringData(offset, len);
}
}
Replace Data in CharacterData
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.rument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
CDATASection cdataNode = doc.createCDATASection("");
Comment commentNode = doc.createComment("");
Text textNode = doc.createTextNode("");
// All three types of nodes implement the CharacterData interface
CharacterData cdata = cdataNode;
cdata = commentNode;
cdata = textNode;
// Replace text
String replacement = "c";
int offset = 10;
int len = 6;
cdata.replaceData(offset, len, replacement);
}
}
Set Data to Character Data
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.rument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setExpandEntityReferences(false);
Document doc = factory.newDocumentBuilder().parse(new File("filename"));
Element element = doc.getElementById("key1");
CDATASection cdataNode = doc.createCDATASection("");
Comment commentNode = doc.createComment("");
Text textNode = doc.createTextNode("");
CharacterData cdata = cdataNode;
cdata = commentNode;
cdata = textNode;
cdata.setData("some data");
int len = cdata.getLength();
}
}
Strip CDATA
public class Utils {
public static String stripCDATA(String s) {
s = s.trim();
if (s.startsWith("<![CDATA[")) {
s = s.substring(9);
int i = s.indexOf("]]>");
if (i == -1) {
throw new IllegalStateException(
"argument starts with <![CDATA[ but cannot find pairing ]]>");
}
s = s.substring(0, i);
}
return s;
}
}