我有一個任務,要求我做一個控制臺應用程式,可以根據用戶的輸入決議一個XML檔案,例如,回傳從xxxx-xxxx生產的汽車的詳細資訊。
Java檔案:
public class Main {
public static void main title function_">main(String[] args) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()。
DocumentBuilder db = dbf.newDocumentBuilder()。
Document doc = db.parse("carls.xml") 。
doc.getDocumentElement().normalize()。
XPath xpath = XPathFactory.newInstance().newXPath() 。
getNode(doc, xpath)。
}
private static void getNode(document doc, XPath xpath) {
XPathExpression expr。
Object result = null。
try {
//如果我可以在這里從Scanner傳遞int值就更理想了。
expr = xpath.compile("/cars/car[production-year[. >= 2015 and . <= 2019]]")。
result = expr.evaluation(doc, XPathConstants.NODESET)。
} catch (XPathExpressionException e) {
e.printStackTrace()。
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i ) {
System.out.println(nodes.item(i).getTextContent())。
}
}
}
我試著使用 Scanner 類并將兩個數字傳給 Expression,但發現它只接受 String 值。
XML檔案:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ? >
<cars>/span>
<car id="1">
<manufacturer>Audi</manufacturer>/span>
<model>/span>Q3</model>/span>
<生產年份>2011</生產年份>
<horsepower>/span>150</horsepower>/span>
<consumption type="fuel">/span>7. 6</consumption>7.
<價格>36430</價格>
</car>/span>
<car id="2">
<manufacturer>雪佛蘭</manufacturer>
<model>/span>Spark</model>/span>
<生產年份>2013</生產年份>
<horsepower> 68</horsepower>
<consumption>/span>6.6</consumption>
<價格>9995</價格>/span>
</car>/span>
<car id="3">
<manufacturer>Aston Martin</manufacturer>
<model>/span>DB11</model>
<生產年份>2013</生產年份>
<horsepower> 510</horsepower>
<consumption type="fuel">/span>13. 5</consumption>13.
<價格>262908</價格>/span>
</car>/span>
<car id="4">
<manufacturer>Volkswagen</manufacturer>
<model>/span>E-Golf</model>/span>
<生產年份>2017</生產年份>
<horsepower> 136</horsepower>
<consumption type="electric"> 0</consumption>
<價格>/span>39250</價格>
</car>/span>
<car id="5">
<manufacturer>Kia</manufacturer>
<model>E-Niro</model>
<生產年份>2019</生產年份>
<horsepower>/span>204</horsepower>/span>
<consumption type="electric"> 0</consumption>
<price>/span>43970</price>
</car>/span>
<car id="6">
<manufacturer>Mazda</manufacturer>
<model>/span>MX-30</model>/span>
<生產年份>2020</生產年份>
<horsepower>/span>145</horsepower>/span>
<consumption type="electric"> 0</consumption>
<price>/span>43970</price>
</car>/span>
<car id="7">
<manufacturer>Mazda</manufacturer>
<model>MX-5 Roadster Coupe</model> /span>
<生產年份>2013</生產年份>
<horsepower> 160</horsepower>
<consumption type="fuel">/span>11. 1</consumption>11.
<價格>40990</價格>
</car>/span>
<car id="8">
<manufacturer>BMW</manufacturer>/span>
<model>X2 SDrive20i</model> /span>
<生產年份>2018</生產年份>
<horsepower>/span>192</horsepower>/span>
<consumption type=" fuel">6. 8</consumption>6.
<價格>47394</價格>/span>
</car>/span>
<car id="9">
<manufacturer>Volkswagen</manufacturer>
<model>/span>T-Roc</model>
<生產年份>2017</生產年份>
<horsepower>/span>116</horsepower>/span>
<consumption type="fuel">/span>5. 8</consumption>5.
<價格>29550</價格>
</car>/span>
<car id="10">
<manufacturer>Toyota</manufacturer>
<model>Corolla Sedan</model>
<生產年份>2019</生產年份>
<horsepower>/span>122</horsepower>/span>
<consumption type="hybrid">/span>3. 1</consumption>3.
<價格> 29860</價格>
</car>/span>
<car id="11">
<manufacturer>達契亞</manufacturer>
<model>/span>Sandero</model>
<生產年份>2016</生產年份>
<horsepower>/span>90</horsepower>/span>
<consumption type="fuel">/span>5. 8</consumption>5.
<價格>14290</價格>
</car>/span>
<car id="12">
<manufacturer>Aston Martin</manufacturer>
<model>V12 Vantage Roadster</model>
<生產年份>2012</生產年份>
<horsepower>/span>517</horsepower>/span>
<consumption type="fuel">/span>24. 3</consumption>24.
<價格>304951</價格>
</car>/span>
<car id="13">
<manufacturer>Fiat</manufacturer>/span>
<model>500e</model>
<生產年份>2020</生產年份>
<horsepower>/span>95</horsepower>/span>
<consumption type="electric"> 0</consumption>
<價格>23900</價格>
</car>/span>
<car id="14">
<manufacturer>Toyota</manufacturer>
<model>GT86</model>
<生產年份>2012</生產年份>
<horsepower> 200</horsepower>
<consumption type=" fuel">11. 5</consumption>11.
<價格>57570</價格>/span>
</car>/span>
<car id="15"/span>>
<manufacturer>Ford</manufacturer>/span>
<model>/span>Grand C-MAX</model>/span>
<生產年份>2015</生產年份>
<horsepower> 125</horsepower>
<consumption type="fuel">/span>6. 4</consumption>6.
<價格>27695</價格>
</car>/span>
</cars>
它不一定要使用XPath,我只是覺得它可能有用,所以我就去做了,但我碰了壁。
uj5u.com熱心網友回復:
你太接近了!
- 你幾乎達到了你的目標。如果你仔細檢查你的
cars.xml,你可以找到一種方法來導航扔檔案中的專案。你可以使用下面的for回圈來導航你的專案,看看會發生什么 。
for (int i = 0。i < nodes. getLength(); i ) {
Node carNode = nodes.item(i)。
NodeList carChildNodes = carNode.getChildNodes() 。
for (int j = 0; j <carChildNodes.getLength(); j ) {
Node carChildNode = carChildNodes.item(j) 。
if (carChildNode.getNodeType() != Node.ELEMENT_NODE)
繼續。
System.out.println(carChildNode.getNodeName() " 。" carChildNode.getTextContent())。
}
//在一個汽車元素和另一個元素之間用一個空行來分隔。
System.out.println()。
}
- 通過運行上述代碼,你的控制臺中的輸出將是:
manufacturer: Volkswagen
模型: E-Golf
生產年份: 2017
馬力: 136
消耗量: 0
價格: 39250
制造商: Kia
型號: E-Niro
生產年份: 2019
馬力: 204
消耗量: 0
價格: 43970
制造商: BMW
型號: X2 SDrive20i
生產年份: 2018
馬力: 192
消耗量: 6.8
價格: 47394
制造商: Volkswagen
模型: T-Roc
生產年份: 2017
馬力: 116
消耗量: 5.8
價格: 29550
. 。
. 。
. .
. .
. .
. .
- 你可以在控制臺提示用戶輸入
fromYear和toYear,只需在你的main方法中插入這段代碼:
Scanner scanner = new Scanner(System.in)。
System.out.println("From Year: ")。
int fromYear = scanner.nextInt()。
System.out.println("To Year: ")。
int toYear = scanner.nextInt()。
uj5u.com熱心網友回復:
找到了答案,當在XPath運算式中使用Scanner輸入時,沒有把正確的連接放在[. >=" firstNum "和. <=" secondNum "]我認為它不能把>=和<=應用到firstNum和secondNum。
public class Main{
public static void main title function_">main(String[] args) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()。
DocumentBuilder db = dbf.newDocumentBuilder()。
Document doc = db.parse("carls.xml") 。
doc.getDocumentElement().normalize()。
XPath xpath = XPathFactory.newInstance().newXPath() 。
getNode(doc, xpath)。
}
private static void getNode(document doc, XPath xpath) {
XPathExpression expr。
Object result = null。
try {
Scanner scanner = new Scanner(System.in);
int firstNum;
int secondNum;
System.out.println("輸入第一個數字:")。
firstNum = scanner.nextInt();
System.out.println("輸入第二個數字:")。
secondNum = scanner.nextInt();
expr = xpath.compile("/cars/car[production-year[. >=" firstNum " and . <=" secondNum " ]")。)
result = expr.evaluation(doc, XPathConstants.NODESET)。
} catch (XPathExpressionException e) {
e.printStackTrace()。
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i ) {
Node carNode = nodes.item(i)。
NodeList carChildNodes = carNode.getChildNodes() 。
for (int j = 0; j <carChildNodes.getLength(); j ) {
Node carChildNode = carChildNodes.item(j) 。
if (carChildNode.getNodeType() != Node.ELEMENT_NODE)
繼續。
System.out.println(carChildNode.getNodeName() " 。" carChildNode.getTextContent())。
}
System.out.println()。
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/321084.html
標籤:
下一篇:為資料創建一個EXCEL表格
