gpt4 book ai didi

java - 从 mysql 导出的 xml 中读取列值

转载 作者:行者123 更新时间:2023-11-29 00:08:03 25 4
gpt4 key购买 nike

我需要读取从 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。任何想法请提出建议。

最佳答案

试试这个肯定有帮助

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();
}
}
}

关于java - 从 mysql 导出的 xml 中读取列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26602606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com