java
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class StringToXMLConverter {
public static void main(String[] args) {
String input = "<chapter xml:id=\"chapter3-1\" role=\"汉语辞书条目库\">" +
"<title xml:id=\"chapter3-1.title\">阿鼻地狱</title>" +
"<info><title>阿鼻地狱</title>" +
"<releaseinfo role=\"拼音\">ā bí dì yù</releaseinfo>" +
"<releaseinfo role=\"来源图书\">新华成语词典</releaseinfo></info>" +
"<para>阿鼻:梵语译音,意为"无间",即痛苦没有间断的意思。"阿鼻地狱"为佛教传说中八大地狱里最下层、最痛苦的地狱。</para>" +
"<para><emphasis role=\"italic\">《敦煌变文集·目连缘起》:"七日之间,母身将死,堕阿鼻地狱,受无间之余殃。"元·无名氏《来生债》四折:"若不是点化真言,险堕了阿鼻地狱。"柯灵《从〈秋瑾传〉说到〈赛金花〉》:"八国联军铁蹄下的故都,烧杀淫掠,如阿鼻地狱,尽人皆知。"</emphasis></para>" +
"<para>也比喻无法摆脱、难以忍受的痛苦境地。</para>" +
"<para><emphasis role=\"italic\">冯雪峰《上饶集中营·炼狱杂记》:"但也有少数意志薄弱的......逐步上当,终至堕入阿鼻地狱。"</emphasis></para>" +
"</chapter>";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
// 创建根节点
Element rootElement = doc.createElement("root");
doc.appendChild(rootElement);
// 将输入转化为DOM节点
Document inputDoc = builder.parse(new InputSource(new StringReader(input)));
Node inputRoot = inputDoc.getDocumentElement();
// 遍历输入节点并复制到结果节点
copyNode(doc, rootElement, inputRoot);
// 格式化输出XML
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyNode(Document doc, Node destParent, Node src) {
if (src == null || destParent == null) {
return;
}
Node newNode = null;
switch (src.getNodeType()) {
case Node.ELEMENT_NODE:
Element srcElement = (Element) src;
Element newElement = doc.createElement(srcElement.getTagName());
destParent.appendChild(newElement);
newNode = newElement;
NamedNodeMap srcAttributes = srcElement.getAttributes();
for (int i = 0; i < srcAttributes.getLength(); i++) {
Attr attr = (Attr) srcAttributes.item(i);
newElement.setAttribute(attr.getName(), attr.getValue());
}
break;
case Node.TEXT_NODE:
newNode = doc.createTextNode(src.getNodeValue());
destParent.appendChild(newNode);
break;
case Node.COMMENT_NODE:
newNode = doc.createComment(src.getNodeValue());
destParent.appendChild(newNode);
break;
default:
break;
}
NodeList srcChildren = src.getChildNodes();
for (int i = 0; i < srcChildren.getLength(); i++) {
copyNode(doc, newNode, srcChildren.item(i));
}
}
}