DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("gb2312"))));
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("gb2312"))));
String xmlString="<xml><group><producelotid>1-1-1</producelotid><weight>20</weight></group> <group><producelotid>1-1-2</producelotid><weight>30</weight></group></xml>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("gb2312"))));
NodeList nodeList=doc.getChildNodes();
for(int i=0;i<nodeList.getLength();i++){
Node node=nodeList.item(i);
String producelotid=node.getFirstChild().getFirstChild().getNodeValue();
String weight=node.getLastChild().getNodeValue();
ht.put(producelotid,weight); }
System.out.println("begin:");
System.out.println(ht);
public static Document string2Dom(String xml)throws ParserConfigurationException, IOException, SAXException {
if (db == null) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
}
return db.parse(new InputSource(new StringReader(xml)));
}
Hashtable ht=new Hashtable();
String xmlString="<xml><group><producelotid>1-1-1</producelotid><weight>20</weight></group> <group><producelotid>1-1-2</producelotid><weight>30</weight></group></xml>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("gb2312"))));
System.out.println(doc);//这里输出为:[#document: null]
NodeList nodeList=doc.getChildNodes();
for(int i=0;i<nodeList.getLength();i++){
Node node=nodeList.item(i);
String producelotid=node.getFirstChild().getFirstChild().getNodeValue();
String weight=node.getLastChild().getNodeValue();
ht.put(producelotid,weight); }
System.out.println("begin:");
System.out.println(ht);
这一句是解析的文件,我的程序中
Document doc = db.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("gb2312"))));
解析字符串好像就不能得到正确的document对象?