• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

setNamespaceAware problem !!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I�m trying to pars un xml file (below), with setting �setNamespaceAware(false)�

My string s is always empty, I do not why.

I someone can help me, thanks in advance,

fshomou@hotmail.com
----------------------

My xml file:
<?xml version="1.0" encoding="UTF-8"?>
<fs:Faris xmlns:fs="http://www.test.com">
<fs:Acknowledgement Receiver="salam" Sender="faris">
<fs:UUId>12322222</fs:UUId>
</fs:Acknowledgement>
</fs:Faris>

my code:
----------------------------------------------------
// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

// DOM
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("C:\\mytest.xml"));
XPath xpath;
xpath = XPathFactory.newInstance().newXPath();

String s = (String)xpath.evaluate("//Acknowledgement//@Receiver", doc,
XPathConstants.STRING);
System.out.println(s);


---------------------------------------------------------------





<?xml version="1.0" encoding="UTF-8"?>
<fs:Faris xmlns:fs="http://www.test.com">
<fs:Acknowledgement Receiver="salam" Sender="faris">
<fs:UUId>12322222</fs:UUId>
</fs:Acknowledgement>
</fs:Faris>
 
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't specify the namespace in your XPath object. I haven't used that class before but I see it has a "setNamespaceContext" method. And I am guessing you would have to include the namespace prefix in your XPath expression too.
 
faris shomou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for yore reponce.

The problem is that i do not know what is the prefix for the namesapace.

I want ust to use the xpath without the namesapace prefix.
 
Paul Clapham
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The namespace prefix is part of the element name, or more precisely the namespace prefix is a surrogate for the namespace URI, which is part of the element name. So you need to know the namespace URI to create an XPath object that can find nodes involving that namespace. I have been looking at the API documentation and it looks to me like you have to do this:Now, I haven't ever tried this, so it is quite likely to have problems. So now it's your turn to hack around with it until you get it to work.

Note that you DO need to know the namespace URI of the element you are searching for. If you don't know that then you have a design problem.
 
faris shomou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do like to thank you again, the problem is each time my client change his namespace�s prefix, so I�m asking if I could eliminate all the namespaces dynamically and the I can use the xpath without prefixes:

String s =
(String)xpath.evaluate("//Acknowledgement//@Receiver", doc, XPathConstants.STRING);
 
Paul Clapham
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Namespace prefix doesn't matter. That's why your client can change it every time. It's the namespace URI that should not change.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic