XPath Select 来自父节点和子节点的属性值
XPath Select Attribute Values from Parent AND Child Nodes
以下是我的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-GB">
<results>
<sector sectorid="1" sectorname="Basic Materials">
<industry id="112" name="Agricultural Chemicals"/>
<industry id="132" name="Aluminum"/>
<industry id="110" name="Chemicals - Major Diversified"/>
<industry id="131" name="Copper"/>
<industry id="134" name="Gold"/>
<industry id="121" name="Independent Oil and Gas"/>
<industry id="120" name="Major Integrated Oil and Gas"/>
</sector>
<sector sectorid="2" sectorname="Conglomerates">
<industry id="210" name="Conglomerates"/>
</sector>
<sector sectorid="7" sectorname="Services">
<industry id="720" name="Advertising Agencies"/>
<industry id="773" name="Air Delivery and Freight Services"/>
<industry id="772" name="Air Services and Others"/>
<industry id="730" name="Apparel Stores"/>
<industry id="744" name="Auto Dealerships"/>
</sector>
</results>
</query>
从上面的 XML 文件中,我希望将属性值存储在适当的变量中:sectorid
、id
和 name
(我'米使用 Java)。我一直在查看不同的 XPath 表达式,并想出了以下代码,但是,在存储 id
属性的值时会抛出 java.lang.NumberFormatException: For input string: ""
异常。这是我的代码:
public class XMLToDatabase {
private static int __SectorID;
private static int __IndustryID;
private static String __IndustryName;
public static void main(String[] args) throws SQLException, UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
try {
File _XMLFile = new File("SectorsAndIndustries.xml");
DocumentBuilderFactory _DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
_DocumentBuilderFactory.setNamespaceAware(true);
DocumentBuilder _DocumentBuilder = _DocumentBuilderFactory.newDocumentBuilder();
Document _Document = _DocumentBuilder.parse(_XMLFile);
_Document.getDocumentElement().normalize();
XPath _XPath = XPathFactory.newInstance().newXPath();
XPathExpression _XPathExpression = _XPath.compile("//sector | //industry");
NodeList _NodeList = (NodeList) _XPathExpression.evaluate(_Document, XPathConstants.NODESET);
for (int i = 0; i < _NodeList.getLength(); i++) {
Node _Node = _NodeList.item(i);
if(_Node.getNodeType() == Node.ELEMENT_NODE) {
Element _Element = (Element) _Node;
__SectorID = Integer.parseInt(_Element.getAttribute("sectorid"));
__IndustryID = Integer.parseInt(_Element.getAttribute("id"));
__IndustryName = _Element.getAttribute("name");
}
System.out.println(__SectorID + ", " + __IndustryID + ", " + __IndustryName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
谁能帮我确定是我弄错了 XPath Expression
,还是我存储第二个变量 __IndustryID
的方式?因为第一个变量 __SectorID
正确存储了值 1
,但是 __IndustryID
抛出了上述异常。理想情况下,我想在每次执行 for
循环时存储所有 3 个属性的值,以将它们保存到数据库 table。如果需要更多信息,请告诉我。
据我所知,您正在编译一个节点列表,其中节点是 sector
或 industry
元素。对于其中的每一个,您都希望检索 sectorid
和 id
属性 - 但显然,没有元素同时具有这两个属性。
更好的方法是
- 找到所有
sector
个元素,并为每个元素打印扇区 ID
- 对于每个
sector
元素遍历其所有称为 industry
的子元素(这需要对每个 sector
元素应用第二个 XPath 表达式,但这是一个微不足道的:"industry"
)
- 并输出每个
industry
的ID属性
Mathias 提出了正确的方法,我想出了一个稍作修改的解决方案:
public class XMLToDatabase {
private static int __SectorID;
private static int __IndustryID;
private static String __IndustryName;
public static void main(String[] args) throws SQLException,
UnsupportedEncodingException, ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
try {
File _XMLFile = new File("C:/Users/Sachin/Desktop/SectorsAndIndustries.xml");
DocumentBuilderFactory _DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder _DocumentBuilder = _DocumentBuilderFactory.newDocumentBuilder();
Document _Document = _DocumentBuilder.parse(_XMLFile);
_Document.getDocumentElement().normalize();
XPath _XPath = XPathFactory.newInstance().newXPath();
NodeList _NodeList1 = (NodeList) _XPath.evaluate("/results/sector", _Document, XPathConstants.NODESET);
for (int i = 0; i < _NodeList1.getLength(); i++) {
Element _Element1 = (Element) _NodeList1.item(i);
__SectorID = Integer.parseInt(_Element1.getAttribute("sectorid"));
NodeList _NodeList2 = (NodeList) _XPath.evaluate("industry", _Element1, XPathConstants.NODESET);
for (int k=0; k < _NodeList2.getLength(); k++) {
__IndustryID = Integer.parseInt(_XPath.evaluate("industry[position()=" + (k + 1) + "]/@id", _Element1));
__IndustryName = _XPath.evaluate("industry[position()=" + (k + 1) + "]/@name", _Element1);
System.out.println(__SectorID + ", " + __IndustryID + ", " + __IndustryName);
}
System.out.println("\n-----------\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
以下是我的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-GB">
<results>
<sector sectorid="1" sectorname="Basic Materials">
<industry id="112" name="Agricultural Chemicals"/>
<industry id="132" name="Aluminum"/>
<industry id="110" name="Chemicals - Major Diversified"/>
<industry id="131" name="Copper"/>
<industry id="134" name="Gold"/>
<industry id="121" name="Independent Oil and Gas"/>
<industry id="120" name="Major Integrated Oil and Gas"/>
</sector>
<sector sectorid="2" sectorname="Conglomerates">
<industry id="210" name="Conglomerates"/>
</sector>
<sector sectorid="7" sectorname="Services">
<industry id="720" name="Advertising Agencies"/>
<industry id="773" name="Air Delivery and Freight Services"/>
<industry id="772" name="Air Services and Others"/>
<industry id="730" name="Apparel Stores"/>
<industry id="744" name="Auto Dealerships"/>
</sector>
</results>
</query>
从上面的 XML 文件中,我希望将属性值存储在适当的变量中:sectorid
、id
和 name
(我'米使用 Java)。我一直在查看不同的 XPath 表达式,并想出了以下代码,但是,在存储 id
属性的值时会抛出 java.lang.NumberFormatException: For input string: ""
异常。这是我的代码:
public class XMLToDatabase {
private static int __SectorID;
private static int __IndustryID;
private static String __IndustryName;
public static void main(String[] args) throws SQLException, UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
try {
File _XMLFile = new File("SectorsAndIndustries.xml");
DocumentBuilderFactory _DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
_DocumentBuilderFactory.setNamespaceAware(true);
DocumentBuilder _DocumentBuilder = _DocumentBuilderFactory.newDocumentBuilder();
Document _Document = _DocumentBuilder.parse(_XMLFile);
_Document.getDocumentElement().normalize();
XPath _XPath = XPathFactory.newInstance().newXPath();
XPathExpression _XPathExpression = _XPath.compile("//sector | //industry");
NodeList _NodeList = (NodeList) _XPathExpression.evaluate(_Document, XPathConstants.NODESET);
for (int i = 0; i < _NodeList.getLength(); i++) {
Node _Node = _NodeList.item(i);
if(_Node.getNodeType() == Node.ELEMENT_NODE) {
Element _Element = (Element) _Node;
__SectorID = Integer.parseInt(_Element.getAttribute("sectorid"));
__IndustryID = Integer.parseInt(_Element.getAttribute("id"));
__IndustryName = _Element.getAttribute("name");
}
System.out.println(__SectorID + ", " + __IndustryID + ", " + __IndustryName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
谁能帮我确定是我弄错了 XPath Expression
,还是我存储第二个变量 __IndustryID
的方式?因为第一个变量 __SectorID
正确存储了值 1
,但是 __IndustryID
抛出了上述异常。理想情况下,我想在每次执行 for
循环时存储所有 3 个属性的值,以将它们保存到数据库 table。如果需要更多信息,请告诉我。
据我所知,您正在编译一个节点列表,其中节点是 sector
或 industry
元素。对于其中的每一个,您都希望检索 sectorid
和 id
属性 - 但显然,没有元素同时具有这两个属性。
更好的方法是
- 找到所有
sector
个元素,并为每个元素打印扇区 ID - 对于每个
sector
元素遍历其所有称为industry
的子元素(这需要对每个sector
元素应用第二个 XPath 表达式,但这是一个微不足道的:"industry"
) - 并输出每个
industry
的ID属性
Mathias 提出了正确的方法,我想出了一个稍作修改的解决方案:
public class XMLToDatabase {
private static int __SectorID;
private static int __IndustryID;
private static String __IndustryName;
public static void main(String[] args) throws SQLException,
UnsupportedEncodingException, ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
try {
File _XMLFile = new File("C:/Users/Sachin/Desktop/SectorsAndIndustries.xml");
DocumentBuilderFactory _DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder _DocumentBuilder = _DocumentBuilderFactory.newDocumentBuilder();
Document _Document = _DocumentBuilder.parse(_XMLFile);
_Document.getDocumentElement().normalize();
XPath _XPath = XPathFactory.newInstance().newXPath();
NodeList _NodeList1 = (NodeList) _XPath.evaluate("/results/sector", _Document, XPathConstants.NODESET);
for (int i = 0; i < _NodeList1.getLength(); i++) {
Element _Element1 = (Element) _NodeList1.item(i);
__SectorID = Integer.parseInt(_Element1.getAttribute("sectorid"));
NodeList _NodeList2 = (NodeList) _XPath.evaluate("industry", _Element1, XPathConstants.NODESET);
for (int k=0; k < _NodeList2.getLength(); k++) {
__IndustryID = Integer.parseInt(_XPath.evaluate("industry[position()=" + (k + 1) + "]/@id", _Element1));
__IndustryName = _XPath.evaluate("industry[position()=" + (k + 1) + "]/@name", _Element1);
System.out.println(__SectorID + ", " + __IndustryID + ", " + __IndustryName);
}
System.out.println("\n-----------\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}