【问题标题】:Retrieve a list of nodes of an XML file in Java在 Java 中检索 XML 文件的节点列表
【发布时间】:2016-03-07 07:45:27
【问题描述】:

我有一个如下的 XML 文件

    <?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <item_number>QWZ5671</item_number>
         <price>39.95</price>
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
         <size description="Large">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
      </catalog_item>
      <catalog_item gender="Women's">
         <item_number>RRX9856</item_number>
         <price>42.50</price>
         <size description="Small">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
         </size>
      </catalog_item>
   </product>
</catalog>

在java中提取特定名称(catalog_item)中的所有节点并创建列表(目录项列表)的最佳方法是什么? 请注意,XML 将包含任何节点列表,我应该能够在其中指定节点名称并提取该名称的所有节点以创建列表。

【问题讨论】:

标签: java xml dom xml-parsing


【解决方案1】:

您可以使用像Jsoup 这样的HTML 解析器下载jar 文件并将其添加到您的项目中。然后执行此操作。

Document document = Jsoup.parse(html);
Elements elements = document.select("catalog_item"); //get everything under catalog_item

for (Element element : elements) {
    String number = element.getElementsByTag("price").text(); // select specific tag
    // select rest of info from tags you need
}

【讨论】:

  • 如果Document.select 遵循基本的XPath 语法,则不能保证它会正确进行选择。另外,你为什么要为一个 OP 不努力的帖子付出这么多努力。
  • 我不熟悉 XPath,但使用 Jsoup 应该可以正常工作。不过,我会承认你的第二点。
【解决方案2】:

我想详细发布我的方法,以帮助在相同情况下需要帮助的人。

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("C:/ProductItems.xml"));
doc.getDocumentElement().normalize();

//Reading all the catelog items and store in a NodeList
NodeList catItemList=doc.getElementsByTagName("catalog_item");

if(catItemList.getLength()>0){      //if there are catelog items 
    for(int itemIndex=0 ; itemIndex < catItemList.getLength() ; itemIndex++){
            Node catalogItem=catItemList.item(itemIndex);

            if (catalogItem.getNodeType() == Node.ELEMENT_NODE) {
                 Element eElement = (Element) catalogItem;
                 String gender = eElement.getAttribute("gender");
            }
    }
}

【讨论】:

    【解决方案3】:

    以下是在 vtd-xml 中进行节点提取的代码。提取逻辑就是你需要填写的...

    import com.ximpleware.*;
    
    public class retrieveNodes{
        public  static  void main(String s[]) throws VTDException,java.io.UnsupportedEncodingException,java.io.IOException{
            VTDGen vg = new VTDGen();
            vg.setLCDepth(5);
            if (!vg.parseFile("input.xml", false))
                return;
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            ap.selectXPath("/catalog/product/catalog_item");
            int i=0;
            while((i=ap.evalXPath())!=-1){
               if (vn.toElement(VTDNav.FIRST_CHILD,"itemNumber")){
                   int j=vn.getText();
                   if (j!=-1)
                      System.out.println("text node ==>"+vn.toString(j);
                   vn.toElement(VTDNav.PARENT);
               }
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多