gpt4 book ai didi

java - 使用 jdom 获取两个 XML 标记之间的内容

转载 作者:行者123 更新时间:2023-12-01 06:10:34 26 4
gpt4 key购买 nike

我正在尝试获取标签 <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:

最佳答案

下面是如何使用 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)));
}
}
}

关于java - 使用 jdom 获取两个 XML 标记之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com