如下一个xml
如何取得Moc name 节点下的 所有的节点name的值? 取得 add,rmv,mod....等等这些值..前提是不考虑是Cmd节点或者Attr节点...在线等...
<?xml version="1.0" encoding="UTF-8"?>
<MocInfo>
    <Moc name="phyNe" displayName="NE instance table" description="Physical NE instance table">
        <Cmd  name="ADD" displayName="Add a physical NE instance" />
        <Cmd  name="RMV" displayName="Delete a physical NE instance" />
        <Cmd  name="MOD" displayName="Modify a physical NE instance" />
        <Cmd  name="LST" displayName="Query a physical NE instance" />+
        <Attr name="esn" displayName="Electronic label of the main control subrack" description="Electronic label of the main control subrack" />
        <Attr name="neName" displayName="NE name" description="Physical NE name" />
        <Attr name="neId" displayName="Physical NE ID" description="Physical NE ID" />
        <Attr name="appNeId" displayName="Service NE ID" description="Service NE ID" />
        <Attr name="neType" displayName="NE type" description="Physical NE type" />
        <Attr name="neVer" displayName="NE version" description="Physical NE version" />
        <Attr name="ftpAddr" displayName="Primary ftp address" description="Primary ftp address" />
        <Attr name="bakFtpAddr" displayName="Secondary ftp address" description="Secondary ftp address " />
        <Attr name="xmppPasswd" displayName="XMPP password" description="XMPP password" />
        <Attr name="interOmSubnetP" displayName="Primary Subnet of interNet OM" description="Primary Subnet of interNet OM" />
        <Attr name="interOmSubnetS" displayName="Secondary Subnet of interNet OM" description="Secondary Subnet of interNet OM" />
        <Attr name="maxProcId" displayName="Maximum process instance ID" description="Maximum process instance ID of the current NE" />
        <Attr name="maxOsId" displayName="Maximum OS instance ID" description="Maximum OS instance ID of the current NE" />
        <Attr name="masterNtpSrv" displayName="Master NTP server" description="Master NTP server" />
        <Attr name="slaveNtpSrv" displayName="Slave NTP server" description="Slave NTP server" />
        <Attr name="dhcpSwitch" displayName="DHCP Server Switch" description="DHCP Server Switch" />
    </Moc>

解决方案 »

  1.   

    先获得Moc下的所有子节点List<Element> MocList=doc.selectNodes("MocInfo/Moc");然后对其进行迭代,你这里只有一个moc就不需要了,直接List<Element> MocListChild=MocList.elements();接着就获得没个节点的所有属性
    for(Element el:MocListChild){
    List<Attribute> atr=el.attributes();
    if(atr.size()>0)
    System.out.println(atr.get(0).getValue());//获得name属性
    }
      

  2.   

    List<Element> MocListChild=MocList.elements();
    修改一下List<Element> MocListChild=MocList.get(0).elements();
      

  3.   

    你这好像是写死了的..不知道有没有属性可以一次性全部取得Moc下面子节点的所有name的集合?
      

  4.   

    需要从http://saxon.sourceforge.net/下载 saxon-he
    import javax.xml.xquery.XQConnection;
    import javax.xml.xquery.XQDataSource;
    import javax.xml.xquery.XQPreparedExpression;
    import javax.xml.xquery.XQResultSequence;public final class XQJQuery {    private static final String DRIVER = "net.sf.saxon.xqj.SaxonXQDataSource";
        private static final String QUERY = "doc('example.xml')//@name//string()";    public static void main(final String[] args) throws Exception {
    // Build a connection to the specified driver.
    XQConnection conn = ((XQDataSource) Class.forName(DRIVER).
     newInstance()).getConnection(); // Prepare the expression with the document and the query.
    XQPreparedExpression expr = conn.prepareExpression(QUERY); // Execute the query.
    XQResultSequence result = expr.executeQuery(); // Get all results of the execution.
    while(result.next()) {
    // Print the results to the console.
    System.out.println(result.getItemAsString(null));
    } // Close the expression.
    expr.close();
    }
    }