Java 当特定文本在内容中匹配时查找标记的属性值
如果标记的子项包含特定文本,我想获取id的值 输入:Java 当特定文本在内容中匹配时查找标记的属性值,java,xml,parsing,dom,sax,Java,Xml,Parsing,Dom,Sax,如果标记的子项包含特定文本,我想获取id的值 输入: <base> <parent id="101" txt="hello"> <child1> <data> search </data> </child1> <child2> <data> send</data> </child2> </parent&
<base>
<parent id="101" txt="hello">
<child1>
<data> search </data>
</child1>
<child2>
<data> send</data>
</child2>
</parent>
<parent id="102" txt="hello">
<child1>
<data> hai </data>
</child1>
<child2>
<data> hey </data>
</child2>
</parent>
</base>
搜索
邮寄
海
嘿
输出:
我正在整个文件中搜索“嘿”文本,所以它应该会返回
id=“102”
我试过的代码片段
if(line.indexOf("<Parent")>= 0)
{
String output="";
Pattern pat = Pattern.compile("id=\".*?\"");
Matcher mat = pat.matcher(line);
if(mat.find())
{
int start=mat.start();
int end=mat.end();
output = line.substring(start+4,end-1);
}
Pattern pat1 = Pattern.compile("<parent"[A-Z](?i)[^.?!]*?\\b(hey)\\b[^.?!]*[.?!]")</parent>");
Matcher mat1 = pat.matcher(line);
if(mat.find())
{
bw.write(output);
}
}
if(line.indexOf(“试试这个:它应该给你文本:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class ReadXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File fXmlFile = new File("Path to your xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("parent");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Parent id : " + eElement.getAttribute("id"));
System.out.println("Parent txt : " + eElement.getAttribute("txt"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
导入org.w3c.dom.Element;
公共类ReadXML{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
试一试{
File fXmlFile=新文件(“xml的路径”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“父项”);
System.out.println(“-------------------------------”;
对于(int-temp=0;temp
试试看{
bw=新的缓冲写入程序(新的文件写入程序(outfilename));
br=新的BufferedReader(新的文件读取器(infilename));
而((line=br.readLine())!=null){
if(line.indexOf)(下面是在中执行此操作的代码
@PritamBanerjee-用我尝试过的脚本更新了为什么所有的反对票对我来说都是合法的?如果我没有错,它将打印所有id值,但是如果id值与子标记中的特定文本匹配,我想要id值。
try{
bw = new BufferedWriter(new FileWriter(outfilename));
br = new BufferedReader(new FileReader(infilename));
while((line=br.readLine())!=null){
if(line.indexOf("<PGBLK")>= 0){
Pattern pat = Pattern.compile("KEY=\".*?\"");
Matcher mat = pat.matcher(line);
if(mat.find()){
int start=mat.start();
int end=mat.end();
output = line.substring(start+5,end-1);
}
}
Pattern pat1 = Pattern.compile(".*?Reference dimensions do not require inspection.*?");
Matcher mat1 = pat1.matcher(line);
if(mat1.find()){
bw.write(output);
bw.newLine();
}
}
}
import com.ximpleware.*;
public class xpathSearch {
public static void main(String s[])throws VTDException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("d:\\xml\\input.txt", false))
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/base/parent/*/data[contains(.,'hey')]/../../@id");
int i;
while ((i=ap.evalXPath())!=-1)
System.out.println("attr id has the value of "+vn.toString(i+1));
}
}