Question

  <?xml version="1.0" encoding="UTF-8"?>
 -<ADOXML adoversion="Version 5.1" username="kvarga" database="adonisdb" time="08:55"   date="30.11.2013" version="3.1">
  -<MODELS>
  -<MODEL version="" applib="ADONIS BPMS BP Library 5.1" libtype="bp" modeltype="Business process model" name="Product development" id="mod.25602">
 -<MODELATTRIBUTES>
 <ATTRIBUTE name="Version number" type="STRING"> </ATTRIBUTE>
 <ATTRIBUTE name="Author" type="STRING">kvarga</ATTRIBUTE>
 <ATTRIBUTE name="Creation date" type="STRING">2013-11-30, 08:50</ATTRIBUTE>
 <ATTRIBUTE name="Date last changed" type="STRING">2013-11-30, 08:54:46</ATTRIBUTE>

 -<INSTANCE name="Business Opportunities census" id="obj.25615" class="Activity">
 <ATTRIBUTE name="Position" type="STRING">NODE x:6.5cm y:10.5cm index:7</ATTRIBUTE>

 <ATTRIBUTE name="External tool coupling" type="STRING"> </ATTRIBUTE>
 <ATTRIBUTE name="Description" type="STRING">I WANT THIS PARA 1</ATTRIBUTE>


 <ATTRIBUTE name="Version number" type="STRING"> </ATTRIBUTE>
 <ATTRIBUTE name="Author" type="STRING">kvarga</ATTRIBUTE>
 <ATTRIBUTE name="Creation date" type="STRING">2013-11-30, 08:50</ATTRIBUTE>
 <ATTRIBUTE name="Date last changed" type="STRING">2013-11-30, 08:54:46</ATTRIBUTE>

 -<INSTANCE name="Business Opportunities census" id="obj.25615" class="Vess">
 <ATTRIBUTE name="Position" type="STRING">NODE x:6.5cm y:10.5cm index:7</ATTRIBUTE>
 <ATTRIBUTE name="Description" type="STRING">I DONT WANT THIS PARA 2</ATTRIBUTE>
 </INSTANCE>


  </MODEL>

  </MODELS>

  </ADOXML>

hey there i am new to xml read! Actually what i need is to read the above kind a xml file so that to get the text

  I WANT THIS PARA 1

i need to obtain only this text my xpath query is:

 String expression = "/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']";

But my query gives me the result as:

 I WANT THIS PARA 1
 I DONT WANT THIS PARA 2    //this line i dont want

so the problem is to only read inside the attribute tag whose name is description and type is string but its paraent tag should be instance and its class should be activity!

I am using this code:

  String expression = "/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']";
   NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
   ArrayList<String> text = new ArrayList<String>();

   int k;
 for (k = 0; k < nodeList.getLength(); k++) {

    String txt=nodeList.item(k).getTextContent();
   txt=txt.replace("[","").replace("]","").replace("#","").replace(":", "").replace("•", "").replace("\n", " ")
       .replace("\u0092", " ").replace("'", "").replace("•", " ").replace("-", " ").replace("’", " ").replace("\n", " ");




  text.add(txt);


}
Was it helpful?

Solution 2

XPath doesn't stop with the first match; instead, it will return all matching elements.

You need to figure out how to be more specific in what you want. Maybe you just want the first INSTANCE?

 //INSTANCE[0]/ATTRIBUTE[@name='Description' and @type='STRING']

Or maybe the description of a specific INSTANCE?

 //INSTANCE[@id='obj.25615']/ATTRIBUTE[@name='Description' and @type='STRING']

But the ID is probably pretty random, so you'll want to use the name of the instance instead:

 //INSTANCE[@name='Business Opportunities census']/ATTRIBUTE[@name='Description' and @type='STRING']

Note that // will recursively search for a match; if you have several MODEL instances with Business Opportunities census, then you'll need to figure out a way to distinguish them as well.

OTHER TIPS

Try this string

String expression = "/ADOXML/MODELS/MODEL/INSTANCE[@class='Activity']/ATTRIBUTE[@name='Description' and @type='STRING']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top