从mysql导出的xml中读取列值

时间:2014-10-28 07:06:59

标签: java mysql

我需要读取从mysql数据库导出的表的列。我的样本xml

<?xml version="1.0" encoding="utf-8" ?> 
+ <!-- - phpMyAdmin XML Dump
- version 3.4.5
- http://www.phpmyadmin.net
-
- Host: localhost
- Generation Time: Oct 28, 2014 at 07:45 AM
- Server version: 5.5.16
- PHP Version: 5.3.8

  --> 
- <pma_xml_export version="1.0" xmlns:pma="http://www.phpmyadmin.net/some_doc_url/">
- <!-- 
    - Structure schemas


  --> 
- <pma:structure_schemas>
- <pma:database name="ArchiveIndexer" collation="utf32_unicode_ci" charset="utf32">
  <pma:table name="classificationl2">CREATE TABLE `classificationl2` ( `SNo` int(4) NOT NULL AUTO_INCREMENT, `L1Name` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `L2Name` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`SNo`) ) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=latin1;</pma:table> 
  </pma:database>
  </pma:structure_schemas>
- <!-- 
    - Database: 'ArchiveIndexer'


  --> 
- <database name="ArchiveIndexer">
- <!--  Table classificationl2 
  --> 
- <table name="classificationl2">
  <column name="SNo">4</column> 
  <column name="L1Name">AC     கலை-கலாசாரம் - ARTS CULTURE</column> 
  <column name="L2Name">AW     விருதுகள் விழாக்கள் -  AWARDS FUNCTIONS</column> 
  </table>
- <table name="classificationl2">
  <column name="SNo">5</column> 
  <column name="L1Name">ACகலை-கலாசாரம் - ARTS & CULTURE</column> 
  <column name="L2Name">BUஅழகிப் போட்டி - BEAUTY CONTESTS</column> 
  </table>
- <table name="classificationl2">
  <column name="SNo">6</column> 
  <column name="L1Name">ACகலை-கலாசாரம் - ARTS & CULTURE</column> 
  <column name="L2Name">DAநடனம் - DANCE</column> 
  </table>
- <table name="classificationl2">
  <column name="SNo">7</column> 
  <column name="L1Name">ACகலை-கலாசாரம் - ARTS & CULTURE</column> 
  <column name="L2Name">DRநாடகம் - DRAMA</column> 
  </table>
 </database>
  </pma_xml_export>

我想知道如何读取并将所有L1name设置为jcombo框,还需要根据选定的L1值检索l2值。我使用jdbc做了这个,但现在我的要求是删除jdbc。有任何想法请建议。

1 个答案:

答案 0 :(得分:1)

试试这肯定会有所帮助

public class XMLreadsample {
public static ArrayList<String> L1names  = new ArrayList();
public static ArrayList<String> L2names  = new ArrayList();

public static void main(String[] args) {

    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new File("classification2.xml"));
        doc.getDocumentElement().normalize();
        System.out.println("Root element of the doc is " +    doc.getDocumentElement().getNodeName());

        NodeList listOfPersons = doc.getElementsByTagName("table");
        int totalPersons = listOfPersons.getLength();
        System.out.println("Total no of columns : " + totalPersons);

        for (int s = 0; s < listOfPersons.getLength(); s++) {

            Node firstPersonNode = listOfPersons.item(s);
            if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                Element firstPersonElement = (Element) firstPersonNode;
                NodeList firstNameList = firstPersonElement.getElementsByTagName("column");
                Element firstNameElement = (Element) firstNameList.item(0);

                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("Sl.NO : " + ((Node) textFNList.item(0)).getNodeValue().trim());

                NodeList lastNameList = firstPersonElement.getElementsByTagName("column");
                Element lastNameElement = (Element) lastNameList.item(1);

                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("L1 Name : " + ((Node) textLNList.item(0)).getNodeValue().trim());
                L1names.add(((Node) textLNList.item(0)).getNodeValue().trim());
                NodeList ageList = firstPersonElement.getElementsByTagName("column");
                Element ageElement = (Element) ageList.item(2);
                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("L2 Name : " + ((Node) textAgeList.item(0)).getNodeValue().trim());
               L2names.add(((Node) textAgeList.item(0)).getNodeValue().trim());
            }//end of if clause
        }//end of for loop with s var
        System.err.println("L1names --> "+L1names);
        System.err.println("L2names --> "+L2names);

        System.err.println("L1names size --> "+L1names.size());
        System.err.println("L2names size --> "+L2names.size());
    } catch (SAXParseException err) {
        System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
        System.out.println(" " + err.getMessage());

    } catch (SAXException e) {
        Exception x = e.getException();
        ((x == null) ? e : x).printStackTrace();

    } catch (Throwable t) {
        t.printStackTrace();
    }
}
}