I am trying to create an XML element by including the name space information as its attribute. My code is as follows,
Element root = new Element("APC_DDF");
root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
root.setAttribute("xsi:noNamespaceSchemaLocation","http://localhost/ddf_schema/apc_ddf_1_6.xsd");
root.setAttribute("ddfid", this.dataHolder.getDDFId());
root.setAttribute("ddfname", this.dataHolder.getDDFName());
root.setAttribute("ddfversion", "1");
root.setAttribute("canremove", "yes");
for some reason I am getting the following error,
"Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalNameException: The name "xmlns:xsi" is not legal for JDOM/XML attributes: XML name 'xmlns:xsi' cannot contain the character ":"."
Please help me with the fixes.
Add namespace declarations to the root element and use the Namespace object instead of including the namespace prefix in the attribute name:
Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.addNamespaceDeclaration(xsi);
root.setAttribute("noNamespaceSchemaLocation","http://localhost/ddf_schema/apc_ddf_1_6.xsd", xsi);
// ...
Related
I was trying to propagate the following xml message to the File Output Node in IBM Integration Bus.
<Resto xmlns = 'https://stackoverflow.com'><Location network_id="5dfweg68h"><Category>Continental</Category></Location></Resto>
Following was my associated Java code to construct the above xml message:
MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "xmlns", "https://stackoverflow.com");
MbElement locationParser = xmlParent.createElementAsLastChild(MbElement.TYPE_NAME, "Location", null);
locationParser.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "network_id", "5dfweg68h");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Category","Continental");
On debugging my application, I got XML write errors. I pondered a bit for the necessary solution, and found that adding namespace prefix would solve the problem. Can't it be done without the addition of a prefix? If no, can anyone guide me, in assigning the necessary value by suggesting the necessary changes to be done in Java compute node?
Note: The string assigned to xmlns should be within single quotes in the xml message.
Use constant MbXMLNSC.NAMESPACE_DECLARATION to declare the namespace:
MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsFirstChild(MbXMLNSC.NAMESPACE_DECLARATION, "xmlns", "https://stackoverflow.com");
If your XSD defines that the XML elements are qualified, you have to set the namespace explicitly. Examples:
xmlParent.setNamespace("https://stackoverflow.com");
locationParser.setNamespace("https://stackoverflow.com");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Category", "Continental").setNamespace("https://stackoverflow.com");
I have a xml like below:
<v2:Root xmlns:v2="www.example.com/xsd/">
<ABC>test data</ABC>
<ABC>test data1</ABC>
<ABC>test data2</ABC>
</v2:Root>
When I'm accessing ABC element using JDOM2, i'm getting the element value in debug like
[Element:ABC[Namespace:"www.example.com/xsd/"]].
That's why i couldn't access the element by just using Xpath expression "//ABC". I'm forced to use expression "/*[local-name()='ABC']".Then it works.
Now, my requirement is to acces the elemnt using expression "//ABC" only. Is there any way?
Thanks in advance for any help.
I think you are mistaken about what your XML actually looks like. I believe you also must have:
xmlns="www.example.com/xsd/"
in there somewhere otherwise your ABC Elements would be in the NO_NAMESPACE namespace (and the ABC toString() method would look like: [Element:ABC] )
So, your XML snippet does not match the ABC Element toString() output.
If you fix your question it will be easier to suggest what your XPath expression should look like.
EDIT, assuming I am right that you have the additional redefinition of the default Namespace, then you can use the following JDOM to get the ABC elements:
XPathFactory xpf = XPathFactory.instance();
Namespace defns = Namespace.getNamespace("defns", "www.example.com/xsd/");
XPathExpression<Element> xpe = xpf.compile("//defns:ABC", Filters.element(), null, defns);
List<Element> abcs = xpe.evaluate(doc);
You should read the following exerpt from the XPath specification carefully:
A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error if the QName has a prefix for which there is no namespace declaration in the expression context.
I have an problem in creating xml with the
<c:condition>
<a:condition>
<fieldName>fieldName</fieldName>
<fieldTest>fieldTest</fieldTest>
<fieldValues>
<fieldValue>fieldValue</fieldValue>
</fieldValues>
</a:condition>
<operator>operator</operator>
<a:condition>
<fieldName>fieldName</fieldName>
<fieldTest>fieldTest</fieldTest>
<fieldValues>
<fieldValue>fieldValue</fieldValue>
</fieldValues>
</a:condition>
</c:condition>
Above is the xml tag given to me.
I need to create this tag using the JDOM/XML in java.
So I am using
Element complexCondition = new Element("c:condition");
code to create "c:condition" tag.
But I am getting error
org.jdom.IllegalNameException: The name "c:condition" is not legal for JDOM/XML elements: Element names cannot contain colons.
So don't have any idea what is going wrong.
As I am new to the xml's and JDOM.
Please help me out with this issue.
c:
is a namespace prefix. you should create your element with namespace. check the constructor :
Element(java.lang.String name, java.lang.String prefix, java.lang.String uri)
Creates a new element with the supplied (local) name and a namespace given by the supplied prefix and URI combination.
I have a XML for which i am writing a servlet to pick up contents from the XML. One such tag is <itunes:author>Jonathan Kendrick</itunes:author>
I need to get author value for this. because of :
I tried using namespace and using escape sequence for : but it did not worked for me.
For rest of other XML elements i am simply using
String link=node.getChildText("link").toString();
I am using Jdom parser
in your XML the sequernce 'itunes:author' represents what's called a Q-Namem a "Qualified Name". In XML it consists of a 'Namespace prefix', and a 'Local Name'. In your example, the namespace prefix is 'itunes', and the 'local name' is 'author'.
What you want is the 'author' element in the namespace linked to the prefix 'itunes'. The actual namespace is normally a full URL. I believe the full URL for your example is probably xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd", but you should check that.
So, the Namespace is "http://www.itunes.com/dtds/podcast-1.0.dtd", it's prefix is declared to be 'itunes' (but it could be something else - the actual prefix name is not technically important...)
You want to get the 'author' in the 'http://www.itunes.com/dtds/podcast-1.0.dtd' Namespace so you want:
String author = node.getChildText("author", Namespace.getNamespace("http://www.itunes.com/dtds/podcast-1.0.dtd"));
For more information on Namespaces check out: http://www.w3schools.com/xml/xml_namespaces.asp
I'm trying to write out a graphML document with XOM in java, but I can't figure out how to get all of the namespace declarations correct. To have valid graphML, I need to have a root element that looks like the following:
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
I've been able to get most of this by doing
Element root = new Element("graphml");
root.setNamespaceURI("http://graphml.graphdrawing.org/xmlns");
root.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
The problem is the last element of this tag, the xsi:schemaLocation. I can't figure out how to express this in XOM. I can't do it as a normal attribute, as that throws an exception(Attribute prefixes must be declared.) and doing it as an additional namespace declaration also results in an exception(NCNames cannot contain colons). Any ideas?
This should do it. Basically you didn't provide the namespace URI for xsi:schemaLocation attribute. Thus trying to create a prefixed attribute with no namespace which clearly won't work.
root.addAttribute(new Attribute("xsi:schemaLocation",
"http://www.w3.org/2001/XMLSchema-instance",
"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"));
Check here for the correct Attribute constructor
Attribute(String name, String URI, String value)