一、使用最原始的javax.xml.parsers,标准的jdk api
// 字符串转XML
-
@H_404_30@StringxmlStr=\"......\";
- @H_404_30@StringReadersr=newStringReader(xmlStr);
- @H_404_30@InputSourceis=newInputSource(sr);
- @H_404_30@DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
- @H_404_30@DocumentBuilderbuilder=factory.newDocumentBuilder();
- @H_404_30@Documentdoc=builder.parse(is);
//XML转字符串
-
@H_404_30@TransformerFactorytf=TransformerFactory.newInstance();
- @H_404_30@Transformert=tf.newTransformer();
- @H_404_30@t.setOutputProperty(\"encoding\",\"GB23121\");//解决中文问题,试过用GBK不行
- @H_404_30@ByteArrayOutputStreambos=newByteArrayOutputStream();
- @H_404_30@t.transform(newDOMSource(doc),newStreamResult(bos));
- @H_404_30@StringxmlStr=bos.toString();
这里的XML DOCUMENT为org.w3c.dom.Document
二、使用dom4j后程式变得更简单
三、使用JDOM
JDOM的处理方式和第一种方法处理很类似
-
@H_404_30@//字符串转XML
- @H_404_30@StringxmlStr=\".....\";
- @H_404_30@StringReadersr=newStringReader(xmlStr);
- @H_404_30@InputSourceis=newInputSource(sr);
- @H_404_30@Documentdoc=(newSAXBuilder()).build(is);
- @H_404_30@
- @H_404_30@//XML转字符串
- @H_404_30@Formatformat=Format.getPrettyFormat();
- @H_404_30@format.setEncoding(\"gb2312\");//配置xml文档的字符为gb2312,解决中文问题
- @H_404_30@XMLOutputterxmlout=newXMLOutputter(format);
- @H_404_30@ByteArrayOutputStreambo=newByteArrayOutputStream();
- @H_404_30@xmlout.output(doc,bo);
- @H_404_30@StringxmlStr=bo.toString();
- @H_404_30@
- @H_404_30@这里的XMLDOCUMENT为org.jdom.Document
四、JAVASCRIPT中的处理
-
@H_404_30@//字符串转XML
- @H_404_30@varxmlStr=\".....\";
- @H_404_30@varxmlDoc=newActiveXObject(\"Microsoft.XMLDOM\");
- @H_404_30@xmlDoc.async=false;
- @H_404_30@xmlDoc.loadXML(xmlStr);
- @H_404_30@//能够处理这个xmlDoc了
- @H_404_30@varname=xmlDoc.selectSingleNode(\"/person/name\");
- @H_404_30@alert(name.text);
- @H_404_30@
- @H_404_30@//XML转字符串
- @H_404_30@varxmlDoc=......;
- @H_404_30@varxmlStr=xmlDoc.xml
- @H_404_30@
- @H_404_30@这里的XMLDOCUMENT为javascript版的XMLDOM