Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java org.w3c.dom.Document.normalize() isn't working as expected

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java org.w3c.dom.Document.normalize() isn't working as expected

    I have the following 2 XML's:
    <root><a><b>some text</b></a></root>
    and
    <root>
    <a>
    <b>some text</b>
    </a>
    </root>

    I load them each in a different org.w3c.dom.Document: ctx1 and ctx2.
    after I call
    ctx1.normalize();
    ctx2.normalize();
    I try to check if element 'a' from ctx1 is equal with element 'a' from ctx2. I get failure because element 'a' from ctx2 is seen as containing 3 children, while 'a' element from ctx1 is seen to contain 1 child.

    Why is this so? I think call to normalize should eliminate this problem but it doesn't.

    N.B. I simplified the XML file for the sake of conciseness. My XML files are containing namespaces too. I think this shouldn't be a problem.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java org.w3c.dom.Document.normalize() isn't working as expected

    Is there any java code for this problem? Something that can be compiled and executed for testing.
    Be sure to wrap all posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java org.w3c.dom.Document.normalize() isn't working as expected

    This is my code:
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
     
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		InputStream is1 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx1.xml");
    		InputStream is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx2.xml");
    		Document ctx1 = db.parse(is1);
    		Document ctx2 = db.parse(is2);
     
    		ctx1.normalize();
    		ctx2.normalize();
     
    		XPathFactory xPathFactory = XPathFactory.newInstance();
    		XPath xPath = xPathFactory.newXPath();
     
    		Element a1 = (Element) getElement(ctx1, xPath, "/root/a");
    		Element a2 = (Element) getElement(ctx2, xPath, "/root/a");
    		System.out.println("a1: " + nodeToString(ctx1, a1));
    		System.out.println("a2: " + nodeToString(ctx2, a2));
    		System.out.println(a1.isEqualNode(a2));
     
     
    	}
     
    	private static Node getElement(Node e, XPath xpath, String path) throws XPathExpressionException {
     
    		XPathExpression expr = xpath.compile(path);
    		return (Node) expr.evaluate(e, XPathConstants.NODE);
     
    	}
     
    	private static String nodeToString(Document ctx, Node n) {
     
    		DOMImplementationLS domImplLS = (DOMImplementationLS) ctx.getImplementation();
    		LSSerializer serializer = domImplLS.createLSSerializer();
     
    		return serializer.writeToString(n);
     
    	}

    And ctx1.xml:
    <code>
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <a>
    <b>some text here </b>
    </a>
    </root>
    </code>
    ctx2.xml:
    <code>
    <?xml version="1.0" encoding="UTF-8"?>
    <root><a><b>some text here</b></a></root>
    </code>

    --- Update ---

    Quote Originally Posted by Norm View Post
    Is there any java code for this problem? Something that can be compiled and executed for testing.
    Be sure to wrap all posted code in code tags.
    It seems to me code tags aren't working. Any hints on this?
    Thanks.
    Last edited by Norm; June 9th, 2020 at 09:02 AM. Reason: Changed code tag <>s to []s

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java org.w3c.dom.Document.normalize() isn't working as expected

    The code is missing the import statements and the class declaration.
    Code tags use []s not <>s
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java org.w3c.dom.Document.normalize() isn't working as expected

    The full class bellow:

    import java.io.IOException;
    import java.io.InputStream;
     
    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.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.ls.DOMImplementationLS;
    import org.w3c.dom.ls.LSSerializer;
    import org.xml.sax.SAXException;
     
    public class XMLNormalizeMain {
     
    	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
     
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		InputStream is1 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx1.xml");
    		InputStream is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx2.xml");
    		Document ctx1 = db.parse(is1);
    		Document ctx2 = db.parse(is2);
     
    		ctx1.normalize();
    		ctx2.normalize();
     
    		XPathFactory xPathFactory = XPathFactory.newInstance();
    		XPath xPath = xPathFactory.newXPath();
     
    		Element a1 = (Element) getElement(ctx1, xPath, "/root/a");
    		Element a2 = (Element) getElement(ctx2, xPath, "/root/a");
    		System.out.println("a1: " + nodeToString(ctx1, a1));
    		System.out.println("a2: " + nodeToString(ctx2, a2));
    		System.out.println(a1.isEqualNode(a2));
     
     
    	}
     
    	private static Node getElement(Node e, XPath xpath, String path) throws XPathExpressionException {
     
    		XPathExpression expr = xpath.compile(path);
    		return (Node) expr.evaluate(e, XPathConstants.NODE);
     
    	}
     
    	private static String nodeToString(Document ctx, Node n) {
     
    		DOMImplementationLS domImplLS = (DOMImplementationLS) ctx.getImplementation();
    		LSSerializer serializer = domImplLS.createLSSerializer();
     
    		return serializer.writeToString(n);
     
    	}
     
    }

    XML for ctx1:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    	<a>
    		<b>some text here</b>
    	</a>
    </root>
    XML for ctx2:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <root><a><b>some text here</b></a></root>

Similar Threads

  1. Set Footer and Header in Document using Java
    By raghu3.ankam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2014, 07:01 AM
  2. Fetching the document mode of IE in java
    By soumyam1990 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2013, 04:43 AM
  3. Drawing in JPanel not working as expected
    By brocode in forum AWT / Java Swing
    Replies: 12
    Last Post: May 23rd, 2013, 12:44 PM
  4. Making the pieces fit together in Java (Document vs File)
    By TenLeftFingers in forum Object Oriented Programming
    Replies: 6
    Last Post: November 19th, 2012, 12:06 PM
  5. InputMismatchException try catch block not working as expected
    By mikhl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 23rd, 2011, 07:02 PM

Tags for this Thread