【问题标题】:getting content between two tags XML using jdom使用jdom在两个标签XML之间获取内容
【发布时间】:2016-03-12 14:16:04
【问题描述】:

我正在尝试获取标签 <catalog> </catalog> 之间的内容,因为这是 xml 文件:

<test1>
</test1>
<catalog>
  <book id="bk101">
  <author>Gambardella, Matthew</author> 
  <title>XML Developer's Guide</title> 
  <genre>Computer</genre> 
  <price>44.95</price> 
  </book>
  <book id="bk102">
  <author>Ralls, Kim</author> 
  <title>Midnight Rain</title> 
  <genre>Fantasy</genre> 
  <price>5.95</price> 
  </book>
</catalog>

结果只有这个:

<book id="bk101">
      <author>Gambardella, Matthew</author> 
      <title>XML Developer's Guide</title> 
      <genre>Computer</genre> 
      <price>44.95</price> 
      </book>
      <book id="bk102">
      <author>Ralls, Kim</author> 
      <title>Midnight Rain</title> 
      <genre>Fantasy</genre> 
      <price>5.95</price> 
      </book>

我开始学习jdom,但我可以达到这个结果,我只知道基本操作,任何人都知道如何做,提前谢谢。 我试试这个:

public static void read() throws JDOMException, IOException{
        SAXBuilder reader = new SAXBuilder();
        Document doc = reader.build(new File("C:\\Users\\HC\\Desktop\\dataset\\book.xml"));
        racine = doc.getRootElement();

        String root = racine.getName();
        Element catalog = racine.getChild("catalog");
        List nodes = catalog.getChildren();
        Iterator i = nodes.iterator();
        while(i.hasNext()){
            Element courant = (Element) i.next();
         // i want here keeping all tags inside the tag catalog
         System.out.print(courant.getContent());

        }

    }

我只得到了这样的结果:

[[Text: 
      ], [Element: <author/>], [Text: 
      ], [Element: <title/>], [Text: 
      ], [Element: <genre/>], [Text: 
      ], [Element: <price/>], [Text: 

   ]][[Text: 
      ], [Element: <author/>], [Text: 
      ], [Element: <title/>], [Text: 
      ], [Element: <genre/>], [Text: 
      ], [Element: <price/>], [Text: 

【问题讨论】:

  • 您用来获取结果的代码是什么?
  • 但那是&lt;catalog&gt;元素的内容!你期待什么结果?
  • 我添加了我写的代码

标签: java xml jdom jdom-2


【解决方案1】:

下面是使用vtd-xml的代码

  import com.ximpleware.*;
    public class getContent {
        public static void main(String s[]) throws VTDException{
            VTDGen vg = new VTDGen();
            if (!vg.parseFile("C:\\Users\\HC\\Desktop\\dataset\\book.xml", false))
                return;
            VTDNav vn = vg.getNav();
            if (vn.toElement(VTDNav.FIRST_CHILD, "book")){
                long l = vn.getContentFragment();
                System.out.println( "book content ==> ");
                System.out.println(vn.toString((int)l, (int)(l<<32)));
            }
        }
    }

【讨论】:

    【解决方案2】:

    试试

    List list = catalog.getChildren("book");
    
        for (int i = 0; i < list.size(); i++) {
    
           Element node = (Element) list.get(i);
    
           System.out.println("Author : " +node.getChildText("author"));
           System.out.println("Title : " + node.getChildText("title"));
           // ... etc
    
        }
    

    【讨论】:

    • 我不想指定子节点的名称:/我想自动执行:(
    猜你喜欢
    • 2012-06-22
    • 2016-04-06
    • 1970-01-01
    • 2012-02-15
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2012-08-15
    相关资源
    最近更新 更多