还是解析xml的问题

sinly227 2008-09-05 02:44:30
- <Primary>
- <Entry>
- <Node label="FunctionCall" isDefault="true">
<SourceLocation path="Class1.cs" line="38" lineEnd="38" colStart="0" colEnd="0" snippet="Class1.cs:38:38" />
</Node>
</Entry>
</Primary>

- <Snippet id="Class1.cs:38:38">
<File>Class1.cs</File>
<StartLine>35</StartLine>
<EndLine>41</EndLine>
- <Text>
- <![CDATA[ adapter.Fill(oDataSet, "Contents");
//Print the records in XML format
Console.WriteLine(oDataSet.GetXml());
///Console.WriteLine(oDataSet.GetXml());///
///Console.WriteLine(oDataSet.GetXml());///
///Console.WriteLine(oDataSet.GetXml());///
]]>
</Text>
</Snippet>
我想通过<Primary>中的snippet="Class1.cs:38:38"这个值来获得对应的text的值即
<![CDATA[ adapter.Fill(oDataSet, "Contents");
//Print the records in XML format
Console.WriteLine(oDataSet.GetXml());
///Console.WriteLine(oDataSet.GetXml());///
///Console.WriteLine(oDataSet.GetXml());///
///Console.WriteLine(oDataSet.GetXml());///
]]>
这个xpath怎么写
要求是先找到snippet="Class1.cs:38:38在找到对应的text
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinly227 2008-09-05
  • 打赏
  • 举报
回复
明白了 原来要查询两个就得用两个xpath啊
不知道啊
受益匪浅啊多谢了
章耿(余淮) 2008-09-05
  • 打赏
  • 举报
回复
这个起始很简单 楼主发了很多类似帖子求代码 为什么不花时间学习下呢???

首先我们读取文件 这个你知道吧
		SAXBuilder sb = new SAXBuilder();
Document doc = sb.build("d:\\d.xml");


然后我们就找到你需要的节点SourceLocation
		XPath xpath = XPath.newInstance("//SourceLocation");
Element SourceLocation = (Element) xpath.selectSingleNode(doc);

如果是多个的花就用List SourceLocations = xpath.selectNodes(doc);

得到节点后我们去取snippet的值
SourceLocation.getAttributeValue("snippet")


取到值我们再去找Snippet节点 我的理解这个是多个的 所以用List
		XPath xpath2 = XPath.newInstance("//Snippet");
List Snippets = xpath2.selectNodes(doc);


得到后我们循环遍历所以的Snippet节点 如果id属性相同就取他的子节点text的值
		for (int i = 0; i < Snippets.size(); i++) {
Element Snippet = (Element) Snippets.get(i);

if (Snippet.getAttributeValue("id").equals(snippet))
System.out.println(Snippet.getChildText("Text"));

}


总的代码如下
package cn.ujjboy.answer.csdn;

import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class ReadXML5 {

public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build("d:\\a.xml");

XPath xpath = XPath.newInstance("//SourceLocation");
Element SourceLocation = (Element) xpath.selectSingleNode(doc);
String snippet = SourceLocation.getAttributeValue("snippet");//取得snippet的值
System.out.println(snippet);

XPath xpath2 = XPath.newInstance("//Snippet");
List Snippets = xpath2.selectNodes(doc);
for (int i = 0; i < Snippets.size(); i++) {
Element Snippet = (Element) Snippets.get(i);
if (Snippet.getAttributeValue("id").equals(snippet))//比较id和snippet的值
System.out.println(Snippet.getChildText("Text"));
}
}
}

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧