0

我正在尝试将 xml 切割成多个部分,然后对其进行一些转换。目前我有这个代码:



    public class XMLStax_xslt {

        static boolean allowStream = false;

        public static void main(String[] args) throws Exception {
            FileInputStream fis = new FileInputStream("SourceExternalFile.xml");        
            XMLInputFactory xmlif = null;
            xmlif = XMLInputFactory.newInstance();
            Source xslt = new StreamSource(new File("myTransformFile.xslt"));

            StreamFilter filter = new StreamFilter() {
                @Override
                public boolean accept(XMLStreamReader reader) {
                    int eventType = reader.getEventType();

                    if ( eventType == XMLEvent.START_ELEMENT )
                    {
                        String currentTag = reader.getLocalName();
                        if (currentTag.equals("wantedTag"))
                        {
                            allowStream = true;
                        }
                    }
                    if ( eventType == XMLEvent.END_ELEMENT )
                    {
                        String currentTag = reader.getLocalName();
                        if (currentTag.equals("wantedTag"))
                        {
                            allowStream = false;
                        }
                    }               
                    return allowStream;
                }
            };
            XMLStreamReader xmlR = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis),filter);
            while (xmlR.hasNext())
            {
                TransformerFactory transformerXSLT = TransformerFactory.newInstance();
                Transformer currentXslt = transformerXSLT.newTransformer(xslt);
                currentXslt.transform(new StAXSource(xmlR), new StreamResult("targetFile.xml"));
            }        
            fis.close();
        }   
    }

当线路return allowStream;更改为return true;. 所以,我需要的只是将我需要的部分发送到转换,因为发送整个 XML 不是一种选择。

我怎样才能做到这一点?

谢谢。

4

1 回答 1

0

问题是我将字符串传递给变压器,而不是整个节点。改变XMLStreamReader就是XMLEventReader诀窍。

这是变化:



    public static void main(String[] args) throws Exception {
            FileInputStream fis = new FileInputStream("SourceExternalFile.xml");        
            XMLInputFactory xmlif = null;
            xmlif = XMLInputFactory.newInstance();
            Source xslt = new StreamSource(new File("myTransformFile.xslt"));

            XMLEventReader xmlR = xmlif.createXMLEventReader(xmlif.createXMLStreamReader(fis));

            TransformerFactory transformerXSLT = TransformerFactory.newInstance();
            Transformer currentXslt = transformerXSLT.newTransformer(xslt);

            while (xmlR.hasNext())
            {
                XMLEvent xmlEvent = xmlR.nextEvent();
                if ( xmlEvent.equals("wantedTag") )
                {
                    currentXslt.transform(new StAXSource(xmlR), new StreamResult("targetFile.xml"));
                }
            }
            xmlR.close();
            fis.close();
        }


于 2012-06-29T21:18:01.643 回答