Results 1 to 4 of 4

Thread: [java] xml

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [java] xml

    Hello,
    I'm on Java and I put a xml in a dom. Now I need to check that all "mytag" as a ddiferent "id" value:

    Qt Code:
    1. <xml........................>
    2. <mttag id="one"............./>
    3. <mttag id="two"............./>
    4. <mttag id="one"............./> //--error
    5. <mttag id="three".............>
    6. <mttag id="three.one"............./>
    7. <mttag id="three.one"............./> //--error
    8. </mtag>
    To copy to clipboard, switch view to plain text mode 

    How can I check it?
    thanks,
    Regards

  2. #2
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [java] xml

    It would probably be better to use XML Schema to validate the document, but here is a programatic approach. Use XPath to select all the ids. Iterate over the ids, adding them to a Set. Before adding the current id, check to see if it already exists -- if it does, then you found a duplicate and can return that the document is invalid. Note that to get all the ids (all the way nested down), you have to change your XPath query expression.

    import java.util.HashSet;
    import java.util.Set;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.w3c.dom.Attr;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;


    public class RunValidateXmlIds {

    public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.newDocument();
    if ( uniqueIds(d) ) {
    System.out.println( " document is valid ");
    } else {
    System.out.println(" document is not valid ");
    }
    }

    public static boolean uniqueIds(Document doc) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    NodeList nl = (NodeList) xp.evaluate("/xml/mttag/@id", doc, XPathConstants.NODESET);
    Set<String> allowedIds = new HashSet<String>();
    for ( int i = 0; i < nl.getLength(); i++ ) {
    Node n = nl.item(i);
    Attr a = (Attr) n;
    String attrValue = a.getNodeValue();
    if ( allowedIds.contains(attrValue) ) {
    // if we ever come here, that means there is a duplicate id
    return false;
    } else {
    allowedIds.add(attrValue);
    }
    }
    return true;
    }

    }

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [java] xml

    thanks but there's something strange with your solution. The method newDocument seems not exist. I wrote it:
    Qt Code:
    1. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    2. DocumentBuilder docBuilder =
    3. docBuilderFactory.newDocumentBuilder();
    4. Document doc = docBuilder.parse (new File( fileName ));
    5. Document d = doc.newDocument(); //this doens't work...
    To copy to clipboard, switch view to plain text mode 

    Why? Maybe it's not necessary?
    Regards

  4. #4
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [java] xml

    you are right - you don't need the newDocument() method. newDocument() does exist for DocumentBuilder, not Document.

    you probably need parse( File ) of DocumentBuilder and you probably don't need newDocument() of DocumentBuilder.

    so does it work for you then?

Similar Threads

  1. [java] finding in a HashSet
    By mickey in forum General Programming
    Replies: 3
    Last Post: 17th November 2008, 14:27
  2. [java] hashTable
    By mickey in forum General Programming
    Replies: 1
    Last Post: 28th July 2008, 22:35
  3. [java] ClassLoader
    By mickey in forum General Programming
    Replies: 4
    Last Post: 28th July 2008, 01:23
  4. [java] write a simple server
    By mickey in forum General Programming
    Replies: 0
    Last Post: 22nd July 2008, 00:59
  5. [java] manipulate a string
    By mickey in forum General Programming
    Replies: 5
    Last Post: 10th July 2008, 20:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.