Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Transformer对象将命名空间自动附加到子元素_Java_Xml_Saxparser_Xmlreader_Transformer - Fatal编程技术网

Java Transformer对象将命名空间自动附加到子元素

Java Transformer对象将命名空间自动附加到子元素,java,xml,saxparser,xmlreader,transformer,Java,Xml,Saxparser,Xmlreader,Transformer,为了对XML文件进行一些更改,我使用以下代码: public boolean run() throws Exception { XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) { public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXExcepti

为了对XML文件进行一些更改,我使用以下代码:

  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();
}
我粘贴了一小部分,效果很好。但差别不大

我希望看到的是:

<cp:rule id="1">
  <cp:conditions>
    <SOMECONDITION/>
  </cp:conditions>
  <cp:actions>
  <allow>
    true
  </allow>
  </cp:actions>
</cp:rule>

真的
我看到的是:

<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>

真的
根据我的模式,处理后的XML也是无效的,下次无法使用

我的问题是,如何防止将此名称空间(如本例所示,)添加到子元素中


提前感谢..

您正在调用
startElement
方法以获取带有错误参数的
allow
标记,我很惊讶您的xml处理器没有为此抛出错误:

super.startElement(uri, localName, "allow", attributeList);
这里,
uri
是作为参数获得的
cp:ruleset
元素的名称空间uri,
localName
ruleset
元素的名称。正确的应该是以下内容,使用空字符串作为命名空间uri,并匹配qname和local name的值

super.startElement("", "allow", "allow", attributeList);
这同样适用于您的其他
startElement
/
endElement
调用,而不是

super.startElement(uri, localName, "cp:rule", attributeList);
应该是

super.startElement(uri, "rule", "cp:rule", attributeList);

您使用错误的参数调用了
allow
标记的
startElement
方法,我很惊讶您的xml处理器没有为此抛出错误:

super.startElement(uri, localName, "allow", attributeList);
这里,
uri
是作为参数获得的
cp:ruleset
元素的名称空间uri,
localName
ruleset
元素的名称。正确的应该是以下内容,使用空字符串作为命名空间uri,并匹配qname和local name的值

super.startElement("", "allow", "allow", attributeList);
这同样适用于您的其他
startElement
/
endElement
调用,而不是

super.startElement(uri, localName, "cp:rule", attributeList);
应该是

super.startElement(uri, "rule", "cp:rule", attributeList);

非常感谢,你纠正了一个巨大的误解。非常感谢,你纠正了一个巨大的误解。