如何在java中读取xml?

时间:2011-10-19 11:38:22

标签: java xml xml-parsing

我有以下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;
} 

但无法获得期待的结果。

由于

4 个答案:

答案 0 :(得分:2)

Jaxb过去一直很好,但对于这种情况可能有点过头了。

Information here

答案 1 :(得分:0)

您可以使用SAXDOM解析器API。

答案 2 :(得分:0)

答案 3 :(得分:0)

http://java.sun.com/developer/Books/xmljava/ch03.pdf

http://www.exampledepot.com/taxonomy/term/374

String[] FindNodeValues(String XMLString,String nodeName,String[] vals)
{
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
Document doc = builder.parse(XMLString);//XML String should contain the XML
Node node = doc.getDocumentElement();
NodeList list =doc.getElementsByTagName(nodeName).item(0).getChildNodes();
for(i=0;i<list.length;i++){vals[i]=list.item(0).getNodeValue();}
return vals;
}

仅将其视为伪代码。它可能有错误;