我知道
<?xml version="1.0" ?>
<Root Name="SMS">
  <command_name>provision</command_name>
  <command_name2>
     <line_name>test333line</line_name>
  </command_name2> 
</Root>
这种格式可以用
        DocumentBuilderFactory xmlDomFactory;
        DocumentBuilder xmlDomBuilder;
        Document xmlDoc;
        xmlDomFactory=DocumentBuilderFactory.newInstance();
        xmlDomBuilder=xmlDomFactory.newDocumentBuilder();
        xmlDoc=xmlDomBuilder.parse(xml);

//提取各项节点数据
Node mobileIdNode = xmlDoc.getElementsByTagName("mobile_id").item(0);
mobileId = mobileIdNode.getChildNodes().item(0).getNodeValue();
取得,但不知到怎么可以
得到以下格式的节点值
<?xml version="1.0" ?>
<Root Name="Message">
<DataHead Name="1" />
<DataBody Name="MsgDATA">
<Node Name="N1" Value="1" />
<Node Name="N2" Value="2" />
<Node Name="N3" Value="3" />
</DataBody>
</Root>
哪位老兄知道,望告诉一声,最好有一段例子,谢谢了

解决方案 »

  1.   

    if(mobileIdNode.getChildNodes().item(0).hasAttributes())
    {
         NamedNodeMap a = mobileIdNode.getChildNodes().item(0).getAttributes();
         for(int i=0;i<a.getLength();i++)
         {
             System.out.print(a.item(i).getNodeName());
             System.out.print(":");
             System.out.println(a.item(i).getNodeValue());
         }
    }
      

  2.   

    public class teststr
    {    public teststr(){} private  DocumentBuilderFactory factory=null;
     private  DocumentBuilder builder=null;
     private  Document doc=null;
     
     public  Document getXmlDoc(String xmlFileName){
      String strExc="";
      try{
       factory = DocumentBuilderFactory.newInstance();
       builder=factory.newDocumentBuilder();
       doc=builder.parse(xmlFileName);
           doc.normalize(); 
          }catch(Exception e){
           strExc=e.toString();
        }
        return(doc);
     }     public static void main(String[] args){
          Document doc;
          try{
          teststr test=new teststr();
          doc=test.getXmlDoc("E:/javacode/123.xml");
          Element restNum=(Element)doc.getElementsByTagName("Node").item(0);
          String s=restNum.getAttribute("Value");
            System.out.println(s); 
    }catch(Exception e){
    e.printStackTrace();}
    }
    }