什么是从XML配置中有效阅读的最佳方式

时间:2017-10-07 10:11:23

标签: xml hashmap config categories accounting

我在互联网上搜索了很多关于Java文件的XML文件,但我没有找到任何正确有效的方法来正确读取XML文件。 也许你们比我更了解XML文件。

  

我想动态阅读这个XML文件。

     

读取XML文件中的所有元素后,在HashMap中添加Category-name属性和短语文本。

喜欢这个

  
      
  1. [购物,购物#1]
  2.   
  3. [购物,商店#2]
  4.   
  5. [购物,商店#3]
  6.   

这是XML文件

mysql> select host,user from mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| %         | zhuxin        |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
| localhost | zhuxin        |
+-----------+---------------+


mysql> show grants for 'zhuxin'@'%'; 
+---------------------------------------------------------------+ 
| Grants for zhuxin@%                                           |
+---------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhuxin'@'%' WITH GRANT OPTION | 
| GRANT ALL PRIVILEGES ON `blogdb`.* TO 'zhuxin'@'%'            |  
+---------------------------------------------------------------+ 

mysql> show grants for 'root'@'%';
+--------------------------------------------------+
| Grants for root@%                                | 
+--------------------------------------------------+ 
| GRANT USAGE ON *.* TO 'root'@'%'                 | 
| GRANT ALL PRIVILEGES ON `blogdb`.* TO 'root'@'%' |  
+--------------------------------------------------+

这是我目前正在使用的代码

<?xml version="1.0"?>
<config>
    <categories>
        <category name="Shopping">
            <phrase>Shop#1</phrase>
            <phrase>Shop#2</phrase>
            <phrase>Shop#3</phrase>
        </category>
    </categories>
</config>

控制台中的输出我从此代码获得的内容

private HashMap< String, String > lookfor = new HashMap< String, String >();

public void readConfig( ) {
    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( );
        DocumentBuilder builder = factory.newDocumentBuilder( );
        Document documentParser = builder.parse( new File( path ) );

        NodeList categories = documentParser.getElementsByTagName( "category" );

        for ( int nodePosition = 0; nodePosition < categories.getLength( ); nodePosition++ ) {
            Element element = ( Element ) categories.item( nodePosition );

            for ( int phrasePosition = 0; phrasePosition < element.getElementsByTagName( "phrase" ).getLength( ); phrasePosition++ ) {

                String currentCategoryName = element.getElementsByTagName( "phrase" ).item( phrasePosition ).getParentNode( ).getAttributes( ).getNamedItem( "name" ).toString( ).replaceFirst( "name=", "" ).replace( '"', ' ' ).replace( " ", "" );
                String currentPhrase = element.getElementsByTagName( "phrase" ).item( phrasePosition ).getTextContent( ).trim( );

                System.out.println( currentCategoryName + " " + currentPhrase );

                lookfor.put( currentCategoryName, currentPhrase );

            }
        }

    } catch ( Exception ex ) {
        ex.printStackTrace( );
    }

}

现在出现问题:

  

我在HashMap中使用category-name添加XML文件的所有短语元素,但只有最后一个插入到HashMap中。

如果我然后打印出HashMap值和密钥生病了

  

[Shop#3] [购物]

我希望得到像这样的所有元素

  

[Shop#1] [购物]

     

[Shop#2] [购物]

     

[Shop#3] [购物]

问候迈克

0 个答案:

没有答案