在特定文本在内容中匹配时查找标记的属性值

时间:2016-03-16 19:02:18

标签: java xml parsing dom sax

如果标签的子项包含特定文本,我想获取id的值。

输入:

<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>

输出:

我正在整个文件中搜索“hey”文本,因此它应该返回     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);
     }
    }

3 个答案:

答案 0 :(得分:1)

试试这个:它应该给你文字:

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();
        }
      }

}

答案 1 :(得分:0)

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();
       }
     } 
   }

答案 2 :(得分:0)

以下是在VTD-XML

中执行此操作的代码
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));

    }
}