我想解析程序中那段String形式展现的xml,将name的属性值和value中的值匹配放到map中,现在的问题是我不知道map中key ,value怎样来对应上这里的name值和value中的值
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;import javax.xml.parsers.*;import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;public class ParseXML {
StringReader rd = null;
InputSource inputSource = null;
public static void main(String[] args) {
ParseXML instance = new ParseXML();
String result = "<?xml version='1.0' encoding='UTF-8'?>"
+ " <workflowFields>"
+ " <field name='AB_TradeFlowCode' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>F51015</value>"
+ "</field>"
+ "<field name='caseId' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>2009021015</value>"
+" </field>"
+ "</workflowFields>";
Map<String, String> map = instance.parse(result);
for (Map.Entry<String, String> m : map.entrySet()) {
//System.out.println("Key:" + m.getKey());
//System.out.println("Value:" + m.getValue());
}
} public Map<String, String> parse(String result) {
Map<String, String> map = new HashMap<String, String>();
try {
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
rd = new StringReader(result);
inputSource = new InputSource(rd);
Document doc = dombuilder.parse(inputSource); Element root = doc.getDocumentElement(); // root node
NodeList nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
String name = null;
Node node = nodes.item(i);
//System.out.println(node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
name = node.getAttributes().getNamedItem("name").getNodeValue();
System.out.println("第"+ i + "个name: " + name);
}
NodeList nodes2 = node.getChildNodes();
//System.out.println(nodes2.item(0).getTextContent());
//map.put(name, nodes2.item(0).getTextContent());
for (int j = 0; j < nodes2.getLength(); j++) {
// 遍历根节点下的所有节点
Node node2 = nodes2.item(j);
map.put(name, node2.getTextContent());
//System.out.println("第"+ j + "个name: " + name);
System.out.println("第"+ j + "个value: " + node2.getTextContent());
}
} } catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;import javax.xml.parsers.*;import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;public class ParseXML {
StringReader rd = null;
InputSource inputSource = null;
public static void main(String[] args) {
ParseXML instance = new ParseXML();
String result = "<?xml version='1.0' encoding='UTF-8'?>"
+ " <workflowFields>"
+ " <field name='AB_TradeFlowCode' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>F51015</value>"
+ "</field>"
+ "<field name='caseId' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>2009021015</value>"
+" </field>"
+ "</workflowFields>";
Map<String, String> map = instance.parse(result);
for (Map.Entry<String, String> m : map.entrySet()) {
//System.out.println("Key:" + m.getKey());
//System.out.println("Value:" + m.getValue());
}
} public Map<String, String> parse(String result) {
Map<String, String> map = new HashMap<String, String>();
try {
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
rd = new StringReader(result);
inputSource = new InputSource(rd);
Document doc = dombuilder.parse(inputSource); Element root = doc.getDocumentElement(); // root node
NodeList nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
String name = null;
Node node = nodes.item(i);
//System.out.println(node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
name = node.getAttributes().getNamedItem("name").getNodeValue();
System.out.println("第"+ i + "个name: " + name);
}
NodeList nodes2 = node.getChildNodes();
//System.out.println(nodes2.item(0).getTextContent());
//map.put(name, nodes2.item(0).getTextContent());
for (int j = 0; j < nodes2.getLength(); j++) {
// 遍历根节点下的所有节点
Node node2 = nodes2.item(j);
map.put(name, node2.getTextContent());
//System.out.println("第"+ j + "个name: " + name);
System.out.println("第"+ j + "个value: " + node2.getTextContent());
}
} } catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;import javax.xml.parsers.*;import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;public class ParseXML {
StringReader rd = null;
InputSource inputSource = null;
public static void main(String[] args) {
ParseXML instance = new ParseXML();
String result = "<?xml version='1.0' encoding='UTF-8'?>"
+ " <workflowFields>"
+ " <field name='AB_TradeFlowCode' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>F51015</value>"
+ "</field>"
+ "<field name='caseId' dataType='String' isArray='' status='' statusDesc='' compareStatus='0'>"
+ "<value>2009021015</value>"
+" </field>"
+ "</workflowFields>";
Map<String, String> map = instance.parse(result);
for (Map.Entry<String, String> m : map.entrySet()) {
System.out.println("Key:" + m.getKey());
System.out.println("Value:" + m.getValue());
}
} public Map<String, String> parse(String result) {
Map<String, String> map = new HashMap<String, String>();
try {
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
rd = new StringReader(result);
inputSource = new InputSource(rd);
Document doc = dombuilder.parse(inputSource);
Element root = doc.getDocumentElement();
NodeList list = root.getElementsByTagName("field");
for(int i = 0;i < list.getLength();i++){
Element element = (Element)list.item(i);
String name = element.getAttribute("name");
String value = element.getElementsByTagName("value").item(0).getTextContent();
map.put(name, value);
} } catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}结果:
Key:caseId
Value:2009021015
Key:AB_TradeFlowCode
Value:F51015