1

I'm trying to extract n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs= using w3c dom

<html>
<div id='token' style='display:none;'>
n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs=
</div>
</html>

but I seem to be stuck

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(con.getInputStream());
NodeList list = doc.getElementsByTagName("div");

Edit:
I got it to work but it seems a little bit clunky:

String token;
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(con.getInputStream());
Element root = doc.getDocumentElement();
NodeList items = root.getElementsByTagName("html");

for(int i = 0; i < items.getLength(); i++) {

    Message message = new Message();
    Node item = items.item(i);
    NodeList properties = item.getChildNodes();

    for(int j = 0; j < properties.getLength(); j++) {
        Node property = properties.item(j);
        String name = property.getNodeName();

        if(name.equalsIgnoreCase("div")) {
            token = property.getFirstChild().getNodeValue());                       
        }

    }

}

Is there a prettier way to get the token?

2
  • is DOM the only option you would consider? Commented May 20, 2010 at 5:49
  • It doesn't matter as long as I get the token Commented May 20, 2010 at 6:18

2 Answers 2

1
VTDGen vg= new VTDGen();

if (vg.parseFile("input.xml",false)){
   VTDNav vn = vg.getNav();
   vn.toElement(VTDNav.FIRST_CHILD);
   int i = vn.getText();
   if (i!=-1)
   System.out.println(" text node is "+vn.toString(i));

}
Sign up to request clarification or add additional context in comments.

Comments

1

In my case VTD-XML parser served the best although my cml documents are not that huge, some sample code using VTD-XML are given below. You can refer the below links which explain that VTD-XML parser is better than SAX, DOM, etc parsers as they have given performance benchmarks as well. http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML http://www.codeproject.com/Articles/24354/VTD-XML-XML-Processing-for-the-Future-Part-II

// For reading xpath values

public String readXpathValue(String dir, String file, String xpath) {
        String value = null;
        try{

            VTDGen vg = new VTDGen();
            int i;
            AutoPilot ap = new AutoPilot();
            ap.selectXPath(xpath);
            if (vg.parseFile(dir+file, true))
            {
                VTDNav vn = vg.getNav();
                ap.bind(vn);
                //XPath eval returns one node at a time
                while ((i = ap.evalXPath()) != -1)
                {
                    value = vn.toString(i);
                }
              //  ap.resetXPath();

            }
        }
        catch (Exception e){
            System.out.println("Exception Occurred in reading Xpath Value : "+e);
        }
        return value; 

    }

// For modifying xml file at runtime

public void formCreateXMLRequest(MAMData mamData,Map<String, String> strTobeModified) throws DatatypeConfigurationException, PayPalUserCreationFailedException, ModifyException, TranscodeException, IOException, XPathEvalException, NavException, XPathParseException
    {
VTDGen vg = new VTDGen();
         if (!vg.parseFile(mamData.getDirectory() + mamData.getBatchRequest(), true))
             return;
         VTDNav vn = vg.getNav();
         XMLModifier xm = new XMLModifier(vn);
         AutoPilot ap = new AutoPilot(vn);

         Set<String> xpathkeys= strTobeModified.keySet();
         for(String xpath : xpathkeys) {


         ap.selectXPath(xpath);
         while((ap.evalXPath()) != -1)
            {
              int p = vn.getText();
              xm.updateToken(p, strTobeModified.get(xpath));
            }

            xm.output(mamData.getDirectory()+mamData.getBatchRequest());
         }
    }

5 Comments

I don't see any answer to the question here.
May I know how u don't see this as an answer, the question was to obtain the data xpath value, and that is what exactly my code example will do.
You have simply dumped code. You have not answered the question. The code should be for a demonstration/example. It's not the answer itself. It might be part of an answer.
I didn't see any specified rules while posting answers, will be careful from next time on. And, from what the question was, I think with slight modification user has the complete answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.