Java 如何将此XML文件解析为MySQL表?
我试图通过如下结构的XML文件进行解析。我试图实现的是将其转换为MySQL中使用的表格式。例如,在本例中,我有一个这样的表,其列格式包含以下示例行:Java 如何将此XML文件解析为MySQL表?,java,sql,xml,Java,Sql,Xml,我试图通过如下结构的XML文件进行解析。我试图实现的是将其转换为MySQL中使用的表格式。例如,在本例中,我有一个这样的表,其列格式包含以下示例行: name | industry_permid | trbc_code | mnemonic Juvenile Products & Accessories | 4294951612 | 5320501015 | NULL Life & Health Insurance | 4294952862 | 55301030| LINS 我
name | industry_permid | trbc_code | mnemonic
Juvenile Products & Accessories | 4294951612 | 5320501015 | NULL
Life & Health Insurance | 4294952862 | 55301030| LINS
我的XML文件:
<conceptSet>
<concept>
<conceptId qcode="B:1389" />
<type qcode="cptType:2" />
<sameAs qcode="P:4294951612" />
<name role="nameRole:main" xml:lang="en">Juvenile Products & Accessories</name>
<broader qcode="B:199" />
<rtr:stage current="stg:live" />
<sameAs qcode="TRBC-2012-Hierarchical-ID:5320501015" />
</concept>
<concept>
<conceptId qcode="B:139" />
<type qcode="cptType:2" />
<sameAs qcode="P:4294952862" />
<name role="nameRole:mnemonic" xml:lang="en">LINS</name>
<name role="nameRole:main" xml:lang="en">Life & Health Insurance</name>
<broader qcode="B:136" />
<rtr:stage current="stg:live" />
<sameAs qcode="TRBC-2012-Hierarchical-ID:55301030" />
</concept>
</conceptSet>
青少年用品及配件
林
人寿和健康保险
问题是,每当我尝试访问这个XML树中的元素时,我只会得到带有名称标记的元素。我不知道如何在没有像qcode之类的标签的情况下访问元素。如果有帮助的话,我将使用默认的JavaXML解析器
这是我到目前为止的代码,每次我尝试获取一个属性时,它都会输出null
File mapping = new File("blah.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(mapping);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("concept");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getAttributes().getNamedItem("qcode"));
}
文件映射=新文件(“blah.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(映射);
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“概念”);
for(int i=0;i
在您的for
-循环中,节点列表。项(i)
是一个概念
元素。因此,您正试图从concept
元素中检索qcode
属性,但它没有
您可以迭代概念
元素的子节点以获得所需的元素,例如:
for (int i = 0; i < nodeList.getLength(); i++) {
NodeList children = nodeList.item(i).getChildNodes();
for (int j = 0; j < children.getLength(); j ++) {
System.out.println(children.item(i).getAttributes().getNamedItem("qcode"));
}
}
for(int i=0;i
或者可以直接使用XPath检索所需的节点,例如,请参见