Java DOMパーサーとApache JxPathを使用してXMLファイルを解析する方法が簡単です。私はDOMパーサー技術を知っていますが、今は私のソースにJxPathを統合しようとしています。JxPathとDOMパーサーを使ってXMLファイルを解析する方法
私はネットワークで検索しましたが、実際の例は見つかりません。
私はこのXMLを持って、テストのために:私はクラスコンテナ、DocumentContainerとJXPathContextのために読んでいるが、 私は感謝するでしょう
File file = new File("Files/catalog.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
NodeList nList = doc.getElementsByTagName("cd");
for(int j = 0; j<nList.getLength(); j++){
Element element = (Element)nList.item(j);
System.out.println(element.getElementsByTagName("artist").item(0).getTextContent() + "\n");
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd gender="male">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd gender="male">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
JavaコードいくつかのヘルプやWebソースのための具体的な動作例。