2016-03-16 2 views
-4

Я хочу получить значение 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> 

Выход:

Я ищу "эй" текста во всем файле, поэтому он должен вернуть идентификатор = "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); 
    } 
    } 
+0

@PritamBanerjee - обновлена ​​с помощью сценария, который я попытался – Karthick

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

} 
+0

Если я не ошибаюсь, он напечатает все значения id, но мне нужно значение id, если оно соответствует определенному тексту в его дочерних тегах. – Karthick

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

    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^