2017-02-17 17 views
0

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ソースのための具体的な動作例。

答えて

2

ここでは、あなたと同様の作業を行う例を示します。

File file = new File("Files/catalog.xml"); 
DocumentContainer dc = new DocumentContainer(file.toURI().toURL()); 
JXPathContext ctx = JXPathContext.newContext(dc); 
Iterator iter = ctx.iterate("//cd/artist"); 
//In this case, following API will return DOM object 
//Iterator iter = ctx.selectNodes("//cd/artist").iterator(); 
while (iter.hasNext()) { 
    System.out.println(iter.next());//object type is String 
} 
1

本当にJXPathが必要ですか? JXPathは、Javaオブジェクトツリーやマップのように、必ずしもXMLではないものに対してXPath式を使用するためのApacheライブラリです。 XMLからDOMを作成した場合は、その上にデフォルトのJava XPath実装を使用することもできます。参照:https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html実際、DOMは要素名のようなメタデータを持つElementTextのようなオブジェクトのツリーを持っているので、JXpathはここであなたに対抗します。従って、//cd/artistのような式を得る代わりに、​​のようなものが得られます。

何らかの理由で、JavaオブジェクトツリーまたはDOMのいずれかになる可能性のあるものに対して、何らかの理由でXPath式を使用する必要がある場合は、その場合はJXPathを使用するとよいでしょうベックヤンの答えは、JXPath DocumentContainerを使ってドキュメントにアクセスすることで説明されています。

0

はい、私は数時間からJXPathはのために読んで、私は今、このAPIのロジックに答えて

多くの感謝を理解しています。ここでいくつかのコードは私を形作る

public class Main { 
    private final static Logger log = Logger.getLogger(Main.class); 

    public static void main(String[] args) { 

     PropertyConfigurator.configure("log4j.properties"); 

     File file = new File("students.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); 

       JXPathContext context = JXPathContext.newContext(element); 
       log.debug(context.getValue("company/address/city[2]")); 

      } 

     } 
     catch (ParserConfigurationException | SAXException | IOException e) 
     { 
      e.printStackTrace(); 
     } 

    } 
関連する問題