XML文件如下:<?xml version="1.0" encoding="UTF-8"?>
<employee><name>lily</name><sex>m</sex><age>29</age></employee>
解析的代码如下: public void parseXML(String fileName) {
File inputXML = new File(fileName);
SAXReader saxReader = new SAXReader();

try {
Document document = saxReader.read(inputXML);
Element employees = document.getRootElement();

for(Iterator i = employees.elementIterator(); i.hasNext();) {
Element employee = (Element)i.next();
// get the nodes 
for(Iterator j = employee.elementIterator(); j.hasNext();) {
Element node = (Element) j.next();
System.out.println(node.getName() + " : " + node.getText());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
解析不出任何内容,最终运行方法后,输出为空。

解决方案 »

  1.   


    List<Element> list = root.getChildren();
    for(Iterator itrt = list.iterator(); itrt.hasNext(); )
    {
         Element e = (Element)itrt.next();
         System.out.println(e.getName() + e.getText());
      

  2.   

    root 即是你的Element employees
      

  3.   

    File inputXML = new File("d:/test.xml");
            SAXReader saxReader = new SAXReader();
            
            try {
                Document document = saxReader.read(inputXML);
                Element employees = document.getRootElement();
                
                for(Iterator i = employees.elementIterator(); i.hasNext();) {
                    Element employee = (Element)i.next();
                    // get the nodes 
                    System.out.println(employee.getName() + " : " + employee.getText());
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
      

  4.   

     public static Item[] parseXml(String xmlpath)
        {
            // 读取./conf/patchmanager/checkdatabase.xml文件中的配置信息
            File xmlfile = new File(xmlpath);
            Document doc;
            Item[] info=null;
            try
            {
                doc = parseFile(xmlfile);
            
            if(null == doc)
            {
                return null;
            }
            Map[] maps =getMapsByPath(doc, "Dev/ItemMarshal/Cat/SubCat/Item", true);
            info= new Item[maps.length];
            for(int i = 0; i < maps.length; i++)
            {
                info[i] = new Item();
                 String name=maps[i].toString();
                 System.out.println(name);
                 
                if(null != (String) maps[i].get("std"))
                {
                    info[i].setStand_key((maps[i].get("std")).toString().trim());
                }
                if(null != (String) maps[i].get("Suggestion"))
                {
                    info[i].setSuggestion_key((maps[i].get("Suggestion")).toString().trim());
                }
            }
            }
            catch (SAXException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return info;
        }
        public static Document parseFile(File file) throws SAXException, IOException
        {
            Document doc = null;
                // 必须要指定builder工厂的具体实例
                DocumentBuilder builder;
                try
                {
                    builder = new DocumentBuilderFactoryImpl()
                            .newDocumentBuilder();
                    doc = builder.parse(file);
                }
                catch (ParserConfigurationException e)
                {
                    e.printStackTrace();
                }
            return doc;
        }
        public static Map[] getMapsByPath(Document doc, String path,
                boolean recursive)
        {
            ArrayList nlist = new ArrayList();
            String[] paths = path.split("/");
            getNodesByPath(nlist, doc.getChildNodes(), paths);        Map[] maps = new HashMap[nlist.size()];
            for (int i = 0; i < maps.length; i++)
            {
                maps[i] = new HashMap();
                Node node = (Node) nlist.get(i);
                NodeList list = node.getChildNodes();
                    for (int k = 0; k < list.getLength(); k++)
                    {
                        if (list.item(k).getNodeType() == Node.ELEMENT_NODE)
                        {
                            if (list.item(k).getChildNodes().item(0) != null)
                            {
                                maps[i].put(list.item(k).getNodeName(), list.item(k).getChildNodes().item(0).getNodeValue());
                            }
    }
    }
    }
    return maps;
    }
    public static void getNodesByPath(ArrayList al, NodeList nl, String[] paths)
    {
    for (int i = 0; i < nl.getLength(); i++)
    {
    if (paths.length > 1 && paths[0] != null
     && nl.item(i).getNodeName().equals(paths[0]))
    {
    Node _node = nl.item(i);
    NodeList list = _node.getChildNodes();
    String[] _paths = new String[paths.length - 1];
    System.arraycopy(paths, 1, _paths, 0, paths.length - 1);
    getNodesByPath(al, list, _paths);
    }
    else if (paths.length == 1 && paths[0] != null
    && nl.item(i).getNodeName().equals(paths[0]))
    {
     al.add(nl.item(i));
    }
    }
    }
    }
      

  5.   

    <?xml version="1.0" encoding="UTF-8"?>
    <Dev Type="RBT_WEBPORTAL_WINDOWS" csv="LangResource.csv" product="RBT">
      <Ver group="RBT_iCheck" name="V100R004">
        <VerDetail>
          V100R004
        </VerDetail>
        <Description>
          The package is used for y.
        </Description>
      </Ver>
      <Interfaces>
        <Intf desc="Command Line Login" name="CLI" port="23|22" protocol="TELNET|SSH">
        <SUNode required="0">
            <NodeDescription>
              Switch User
            </NodeDescription>
            <UserDescription>
               device asthe user for Command Line Login.Then the Breeze iCheck runs the round inspection package.
            </UserDescription>
            <PwdDescription>
             e current user after logging in to the device asthe user for Command Line Login.Then the Breeze iCheck runs the round inspection package.
            </PwdDescription>
          </SUNode> 
           </Intf>
      </Interfaces>
      <ItemMarshal>
      <Cat id="1" name="Running situation of WEBPORTAL applications">
          <SubCat id="1" name="Running situation of WEBPORTAL applications">
            
            <Item CCCut="1" CCSelected="1" GlobalNo="SSKR00C" QCCut="1" QCSelected="1" checkType="Manual" id="1" name="Version directory name " score="1">
              <std>Check the name of the directory where the version is installed.The name of the version directory should be consistent with the version number. For example, if the version number is RBT V5.0D405+ USDP V100R001D505, the version directory name of PORTAL/USDP is portalv50d405/usdpd505 (The directory name cannot contain blanks or other special characters. The character requirement is the same as that described in the installation guide, that is, the installation path of the PORTAL/USDP can contain only English characters, numerals, and underscores. It is recommended that the path consist of up to 61 characters.).
              </std><Suggestion>During the service implementation, perform the setting as required. The modification after the service implementation requires the suspension of the service.
    You can contact Huawei's technical support engineers for help or access http://gcrms-ovs.huawei.com to submit a defect report.
              </Suggestion>
            </Item>
            <Item CCCut="1" CCSelected="1" GlobalNo="SSKR01C" QCCut="1" QCSelected="1" checkType="Automatic" id="2" name="CPU occupancy " score="1">
            <std>
                Use the task manager of Windows to check CPU occupancy. The CPU occupancy should be less than 40%. 
    </std>
              <Suggestion>
                1. View the CPU usage of the processes in the Task Manager. If the CPU usage of the Java process is too high, monitor the process and stop the unnecessary processes.
    If the fault is rectified, go to step 3.
    If the fault persists, go to step 2.
    2. Contact Huawei's technical support engineers for help or access http://gcrms-ovs.huawei.com to submit a defect report.
    3. End.
                            
              </Suggestion>
            </Item>
          </SubCat>
          <SubCat id="2" name="Cruise Inspection"/>
        </Cat>
      </ItemMarshal>
    </Dev>
      

  6.   

     System.out.println(node.getAttributes().getNamedItem("GlobalNo")+"   "+node.getAttributes().getNamedItem("name"));
      

  7.   

    你的xml文件和你写的程序不符。如果是你现在的程序,那你就多写了一层循环;如果想用你现在的程序,那就在你的xml文件最外层加一个<employees></employees>标签。
      

  8.   


    写错了,应该是“如果是你现在的xml文件”