How to get namespace of xml - java

I have this xml:
InputStream is = new ByteArrayInputStream(
"<data:RobCtiAifoData xmlns:data=\"urn:cz:isvs:rob:schemas:RobDotazyData:v1\" xmlns:reg=\"urn:cz:isvs:reg:schemas:RegTypy:v1\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:cz:isvs:rob:schemas:RobUnivDotazy:v1\"><data:Aifo a=\"b\">1</data:Aifo><data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti> </data:RobCtiAifoData>"
.getBytes());
// InputStream is = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
Node node = document.getDocumentElement();
when wanna get name of element without namespace so I wanna call substring of name of element withou prefix
node.getNodeName() gives me data:VyuzitiPoskytnuti
and node.getNamespaceURI() or node.getPrefix() gives me just null. So how I can get prefix of node ?

Try enabling namespace support:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
From the JavaDoc for setNamespaceAware:
Specifies that the parser produced by this code will provide support for XML namespaces. By default the value of this is set to false

Related

xml parse - xpath clarification in Java

How should i get the Link value from the below xml
XML Content
<document-instance system="abc.org" number-of-pages="6" desc="Drawing" link="www.google.com">
<document-format-options>
<document-format>application/pdf</document-format>
<document-format>application/tiff</document-format>
</document-format-options>
<document-section name="DRAWINGS" start-page="1" />
</document-instance>
i traverse update desc attribute after that i'm struggle
XPathExpression firstPageUrl = xPath.compile("//document-instance/#desc=\"Drawing\"]");
Expected output : retrieve the Link value
www.google.com
File file = new File("path to file");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//document-instance/#link";
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
String url= node.getTextContent();

Error writing a XML file in java

I am trying to write an xml file using Java and I want to send a variable string to the method document.createElement(var) ,but it gives me that error:
"Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(Unknown Source)
at eg.edu.alexu.csd.oop.draw.WriteXML.<init>(WriteXML.java:47)
at eg.edu.alexu.csd.oop.draw.Test.main(Test.java:25)"
this error because var is a variable
public class WriteXML {
public WriteXML() throws FileNotFoundException, ParserConfigurationException, IOException{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.newDocument();
for(int i=0 ; i<shapes.length ; i++){
//class
s = shapes[i].getClass().getName().toString();
Element rootElement = xmlDoc.createElement(s);
}
A number cannot be used as a tag name. Here is a simplified test:
// Generates the error
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.newDocument();
xmlDoc.createElement("0");
You can use an attribute, or text content, to set the number.
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.newDocument();
xmlDoc.createElement("Red").setTextContent("0");
XML Specification
I am not sure, but I think the related line is:
[Definition: A Name is an Nmtoken with a restricted set of initial characters.] Disallowed initial characters for Names include digits, diacritics, the full stop and the hyphen.

Comparing of two XML DOC is getting fail because docs is comimg from two source

I have converted a string to an XML document using the code below:
String xmlStr = "<msg><uuid>12345</uuid></msg>"
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
return doc;
} catch (Exception e) {
throw new RuntimeException(e);
}
Then I converted an XML file to a document with the following:
File file = new File("src/test/resources/xmlForJunitTest.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document expectedDoc = db.parse(file);
Finally I compare the two documents:
Document actualDoc = XmlUtils.convertStringToDocument(xmlString);
Diff myDiff = new Diff(expectedDoc, actualDoc);
assert (myDiff.similar());
This test passes using an XML file (xmlForJunitTest.xml) formatted like so:
<msg><uuid>12345</uuid></msg>
And it fails with this:
<msg>
<uuid>12345</uuid>
</msg>
Please you can suggest why this failure occurs, and what the solution is?
The assertion fails because one document includes whitespace, and the other doesn't. I believe you need to look at the normalizeWhitespace flag in XmlUnit (assuming that's what you're using).

Parsing a SOAP response is returning null

String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http://applicant.ffe.org/exchange/1.0'>...</exch:Response></soap:Body></soap:Envelope>";
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
org.w3c.dom.Document document = null;
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
document = db.parse(is);
} catch(ParserConfigurationException e){}
catch(SAXException e){}
document is returning with null. I have tried different ways to pass to InputSource, but document is still returning null. Any idea why this might be happening?
I just tried i could get the elements name and values .
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
document = db.parse(is);
System.out.println(document);//here we get null;
System.out.println(document.getNodeName());//here we get document;
for(int i =0 ; i<document.getChildNodes().getLength();i++)
System.out.println(document.getChildNodes().item(i).getChildNodes().item(i).getNodeName());
}
Output :
[#document: null]
document
soap:Body
To parse SOAPResponse we can javax.xml.soap.* it may take u to traverse the object xml tree. Anyway we may need parse the elements from SOAP Body . we could parse these very simple manner using DOM format .

DocumentBuilder parsing error with xml

I have a XML in String variable and I want to pass that value into the parse() of DocumentBuilder.
String xml = "<?xml version="1.0 ....>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
But, seems like the last line doesnt work as doc is null.
Is there anything wrong with this? I am just trying take a xml value and convert it to Document.
Thanks for help.
You need something nearer
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
You can trim this to:
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
Note that normally you'd expect an XML File and it'd be:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(fullpathtofile));

Categories

Resources