у меня есть следующий xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<processid>processid_1111</processid>
<suggestion>
<geo1>a</geo1>
<geo2>b</geo2>
<geo3>c</geo3>
<standarddatasuggestion>account1</standarddatasuggestion>
</suggestion>
<suggestion>
<geo1>d</geo1>
<geo2>e</geo2>
<geo3>f</geo3>
<geo4>g</geo4>
<standarddatasuggestion>account2</standarddatasuggestion>
</suggestion>
<taskid>taskid_111</taskid>
</root>
Я хочу разработать метод, который может принимать имя узла и на основе имени узла возвращать соответствующий узел и его значение в объектах карты. Пожалуйста, помогите мне разобраться с вышеуказанной проблемой.
Я попробовал:
public SortedMap[] getObjectValueFromXML(Object xmlObject)
{
Node item = null;
SortedMap[] map = null;
String subchildnodeName = null;
String subchildnodeValue = null;
Map subchildmap[] = null;
try {
if (xmlObject == null) {
throw new NullPointerException("XML object found null");
}
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(xmlObject.toString().getBytes()));
doc.getDocumentElement().normalize();
node = doc.getDocumentElement();
root = node.getNodeName();
nList = doc.getElementsByTagName(root);
for (int temp = 0; temp < nList.getLength(); temp++) {
nNode = nList.item(temp);
NodeList childNodes = nNode.getChildNodes();
int length = childNodes.getLength();
map = new TreeMap[length];
String nodeValues[][] = new String[length][2];
for (int i = 0; i < length; i++) {
item = nNode.getChildNodes().item(i);
String parentnodeName = item.getNodeName();
int noderepetationlength = doc.getElementsByTagName(parentnodeName).getLength();
// System.out.println(noderepetationlength);
NodeList rootchildNodes = item.getChildNodes();
String parentnodeValue = rootchildNodes.item(0).getNodeValue();
map[i] = new TreeMap();
if (parentnodeName != null && parentnodeValue != null && noderepetationlength <= 1) {
map[i].put(parentnodeName, parentnodeValue);
} else
{
subchildmap = new HashMap[noderepetationlength];
for (int t = 0; t < noderepetationlength; t++)
{
NodeList childNodes1 = item.getChildNodes();
int length1 = childNodes1.getLength();
subchildmap[t] = new HashMap();
for (int j = 0; j < length1; j++)
{
Node item2 = childNodes1.item(j);
if (item2.getNodeType() == Node.ELEMENT_NODE)
{
subchildnodeName = item2.getNodeName();
NodeList childNodes2 = item2.getChildNodes();
int length2 = childNodes2.getLength();
for (int k = 0; k < length2; k++)
subchildnodeValue = childNodes2.item(k).getNodeValue();
}
subchildmap[t].put(subchildnodeName, subchildnodeValue);
}
map[i].put(parentnodeName, subchildmap[t]);
}
}
}//else
} //end of for-loop
} catch (ParserConfigurationException pce) {
} catch (NullPointerException nofe) {
} catch (SAXException saxException) {
} catch (IOException ioe) {
}
return map;
}
, но не удалось получить ожидаемый результат.
Спасибо