请问如何处理XML格式的字符串 好象没有其它的“简便”方法了,如果有,俺也想知道为什么要将XML文件转化成字串? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这样是我们跟远程通讯,传输格式就是XML,当我通过SOCKET拿到了XML格式的数据,不希望操作I/O,我希望直接分析,然后数据库操作就好了 用Xml String构建InputStream再由InputStream构建InputSource //:parseString.javaimport java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.io.Reader;import java.io.StringReader;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;//import com.ibm.xml.parsers.*;import com.sun.xml.parser.Parser;import com.sun.xml.tree.XmlDocumentBuilder;/** * A sample DOM writer. This sample program illustrates how to * traverse a DOM tree. */public class parseString{ public void parseAndPrint(InputSource xmlSource) { Document doc = null; try { //sun的解析器. http://java.sun.com/xml XmlDocumentBuilder builder = new XmlDocumentBuilder(); Parser parser = new com.sun.xml.parser.Parser(); parser.setDocumentHandler(builder); builder.setParser(parser); builder.setDisableNamespaces(false); parser.parse(xmlSource); doc = builder.getDocument(); /* //IBM的XML4j解析器。http://www.alphaworks.ibm.com DOMParser parser = new DOMParser(); parser.parse(xmlSource); doc = parser.getDocument(); */ } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } // We've parsed the document now, so let's print it. if (doc != null) printDOMTree(doc); } /** Prints the specified node, recursively. */ public void printDOMTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println("<?xml version=\"1.0\" ?>"); printDOMTree(((Document)node).getDocumentElement()); break; } // print element with attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } System.out.println(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName()); String data = node.getNodeValue(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName()); System.out.print('>'); } } // printDOMTree(Node) /** Main program entry point. */ public static void main(String argv[]) { parseString ps = new parseString(); //这里有很多种方式得到xml字符串 StringReader sr = new StringReader("<?xml version=\"1.0\"?><a>Alpha<b>Bravo</b><c>Charlie</c></a>"); InputSource iSrc = new InputSource(sr); ps.parseAndPrint(iSrc); }} 同意tangshancheng(98007),这样的遍历是可以取得xml字符串 1.5264099814E10转化为15264099814 求一个基于RMI的应用小程序 谁来帮我讲解一下这道题~~~谢谢 急,急,急!!!在字符串中添加分隔符 继承问题 如何获得系统时间? 散分了!!今天第一天正式上班啦。。。.. CyberH() 朋友请进!!有事请问 谢谢!! 分页打印的问题 怎么图形化设置classpath 如何让swing中的JOptionDialog弹出的对话框显示的是YES 而不是“确定” ant 编译的错误?
当我通过SOCKET拿到了XML格式的数据,不希望操作I/O,我希望直接分析,然后数据库操作就好了
再由InputStream构建InputSource
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Reader;
import java.io.StringReader;import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
//import com.ibm.xml.parsers.*;
import com.sun.xml.parser.Parser;
import com.sun.xml.tree.XmlDocumentBuilder;/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/public class parseString
{
public void parseAndPrint(InputSource xmlSource)
{
Document doc = null; try
{
//sun的解析器. http://java.sun.com/xml
XmlDocumentBuilder builder = new XmlDocumentBuilder();
Parser parser = new com.sun.xml.parser.Parser();
parser.setDocumentHandler(builder);
builder.setParser(parser);
builder.setDisableNamespaces(false);
parser.parse(xmlSource);
doc = builder.getDocument();
/*
//IBM的XML4j解析器。http://www.alphaworks.ibm.com
DOMParser parser = new DOMParser();
parser.parse(xmlSource);
doc = parser.getDocument();
*/
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
} // We've parsed the document now, so let's print it. if (doc != null)
printDOMTree(doc);
} /** Prints the specified node, recursively. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println("<?xml version=\"1.0\" ?>");
printDOMTree(((Document)node).getDocumentElement());
break;
} // print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
System.out.println(">"); NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
} break;
} // handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
} // print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
} // print text
case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
} // print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print(" ");
System.out.print(data);
}
System.out.print("?>");
break;
}
} if (type == Node.ELEMENT_NODE)
{
System.out.println();
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
} // printDOMTree(Node) /** Main program entry point. */
public static void main(String argv[])
{
parseString ps = new parseString();
//这里有很多种方式得到xml字符串
StringReader sr = new StringReader("<?xml version=\"1.0\"?><a>Alpha<b>Bravo</b><c>Charlie</c></a>");
InputSource iSrc = new InputSource(sr);
ps.parseAndPrint(iSrc);
}
}