Results 1 to 4 of 4

Thread: Help with xml

  1. #1
    Jack is offline Senior Member
    Join Date
    Jun 2007
    Posts
    130
    Rep Power
    0

    Default Help with xml

    I have a problem when I want to do searches in java. It returns some kind of exception that says "the root is empty" I have the same problem when I want to delete elements in a XML
    Can you send me an example ? I don't know how can i do that.
    Thank you guys.

  2. #2
    Heather is offline Senior Member
    Join Date
    Jun 2007
    Posts
    164
    Rep Power
    9

    Default

    This is strange.
    Can you send the exact error ?

  3. #3
    bbq
    bbq is offline Senior Member
    Join Date
    Jun 2007
    Posts
    132
    Rep Power
    0

    Default

    please start to this tutorial
    Working with XML: The Java/XML Tutorial

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    10

    Default

    You could use xpath to search through an XML document.

    Here is an example:

    Java Code:
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringComments(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document doc = parser.parse(yourfile.xml);
    
    String text = "Text to search for";
    			
    // look for the text somewhere in a nested Element
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList)xpath.evaluate("//*[contains(text(),'" + text + "')]", doc, XPathConstants.NODESET);
    
    for (int i = 0, n = nodes.getLength(); i < n; i++) {
    Node node = nodes.item(i);
    
    String path;
    path = node.getNodeName() + '/' + node.getTextContent()
    
    }
    This should search through the XML and return any values that contain your search text.
    Did this post help you? Please me! :cool:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •