Reputation: 129
In order to make some changes on XML file, I use the code below:
public boolean run() throws Exception {
XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if(AddRuleReq&& qName.equalsIgnoreCase("cp:ruleset"))
{
attributeList.clear();
attributeList.addAttribute(uri, localName, "id", "int", Integer.toString(getNewRuleId()));
super.startElement(uri, localName, "cp:rule", attributeList);
attributeList.clear();
super.startElement(uri, localName, "cp:conditions", attributeList);
super.startElement(uri, localName, "SOMECONDITION", attributeList);
super.endElement(uri, localName, "SOMECONDITION");
super.endElement(uri, localName, "cp:conditions");
super.startElement(uri, localName, "cp:actions", attributeList);
super.startElement(uri, localName, "allow", attributeList);
super.characters(BooleanVariable.toCharArray(), 0, BooleanVariable.length());
super.endElement(uri, localName, "allow");
super.endElement(uri, localName, "cp:actions");
}
}
};
Source source = new SAXSource(xr, new InputSource(new StringReader(xmlString)));
stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
transformer.transform(source, result);
return stringWriter.toString();
}
I have pasted a little part of it and it works. But with little differences.
What I expect to see is:
<cp:rule id="1">
<cp:conditions>
<SOMECONDITION/>
</cp:conditions>
<cp:actions>
<allow>
true
</allow>
</cp:actions>
</cp:rule>
What I see is:
<cp:rule id="1">
<cp:conditions>
<SOMECONDITION xmlns="urn:ietf:params:xml:ns:common-policy"/>
</cp:conditions>
<cp:actions>
<allow xmlns="urn:ietf:params:xml:ns:common-policy">
true
</allow>
</cp:actions>
</cp:rule>
The processed XML is also invalid according to my schema and unusable for the next time.
My question is that, how can I prevent this namespaces (as in this example, < SOMECONDITION xmlns="urn:ietf:params:xml:ns:common-policy"/>) being added to child elements?
Thanks in advance..
Upvotes: 1
Views: 495
Reputation: 34014
You are calling the startElement
method for the allow
tag with the wrong parameters, I'm surprised your xml processor is not throwing an error for this:
super.startElement(uri, localName, "allow", attributeList);
Here uri
is the namespace uri of the cp:ruleset
element which you got as a parameter, and localName
is the name of the ruleset
element. Correct should be the following, using an empty string as the namespace uri and matching values for qname and local name.
super.startElement("", "allow", "allow", attributeList);
The same applies to your other startElement
/endElement
calls, instead of
super.startElement(uri, localName, "cp:rule", attributeList);
It should be
super.startElement(uri, "rule", "cp:rule", attributeList);
Upvotes: 1