使用JDOM Java拆分XML

时间:2016-11-15 07:00:23

标签: java xml xpath saxon jdom

我有以下XML字符串。

<Engineers>
    <Engineer>
        <Name>JOHN</Name>
        <Position>STL</Position>
        <Team>SS</Team>
    </Engineer>
    <Engineer>
        <Name>UDAY</Name>
        <Position>TL</Position>
        <Team>SG</Team>
    </Engineer>
    <Engineer>
        <Name>INDRA</Name>
        <Position>Director</Position>
        <Team>PP</Team>
    </Engineer>
</Engineers>

当Xpath作为Engineers / Enginner给出时,我需要将这个xml拆分成更小的xml字符串。

较小的xml字符串如下

    <Engineer>
        <Name>INDRA</Name>
        <Position>Director</Position>
        <Team>PP</Team>
    </Engineer>

<Engineer>
        <Name>JOHN</Name>
        <Position>STL</Position>
        <Team>SS</Team>
 </Engineer>

我已经使用saxon xpath和JDOM实现了以下功能。

import net.sf.saxon.Configuration;
import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.xpath.XPathFactoryImpl;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.junit.Test;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;

 public void testXML() throws XPathFactoryConfigurationException, XPathExpressionException, Exception {

        System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_JDOM,
                "net.sf.saxon.xpath.XPathFactoryImpl");
        XPathFactory xPathFactory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_JDOM);
        XPath xPath = xPathFactory.newXPath();
        InputSource inputSource = new InputSource(new File(filename).toURI().toString());
        SAXSource saxSource = new SAXSource(inputSource);
        Configuration config = ((XPathFactoryImpl) xPathFactory).getConfiguration();
        DocumentInfo document = config.buildDocument(saxSource);
        XPathExpression xPathExpression = xPath.compile("//Engineers/Engineer");
        List matches = (List) xPathExpression.evaluate(document, XPathConstants.NODESET);
        if (matches != null) {
            for (Iterator iter = matches.iterator(); iter.hasNext(); ) {
                NodeInfo node = (NodeInfo) iter.next();
                System.out.println(node.getDisplayName() + " - " + node.getStringValue());
            }
        }

    }

它给出了以下结果。

Engineer - 
        JOHN
        STL
        SS

Engineer - 
        UDAY
        TL
        SG

Engineer - 
        INDRA
        Director
        PP

如何更改代码以便获得所需的输出?或者是否可以在工程师中获取子属性(名称,位置,团队)的名称

2 个答案:

答案 0 :(得分:1)

如果您正在使用JDOM进行此项工作,则应考虑使用本机JDOM方法而不是通过Saxon运行抽象。

考虑类似的事情:

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.XPAthExpression;
import org.jdom2.output.XMLOutputter;
import org.jdom2.input.SAXBuilder;
import org.jdom2.filter.Filters;

....

    XPathExpression xpe = XPathFactory.instance()
         .compile("//Engineers/Engineer", Filters.element());

    Document doc = new SAXBuilder().build(new File(filename));

    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());

    for (Element e : xpe.evaluate(doc)) {
        xout.output(e, System.out);
    }

答案 1 :(得分:1)

我会在XSLT中进行拆分:

<xsl:stylesheet ....>
<xsl:template match="Engineeers/Engineer">
  <xsl:result-document href="{position()}.xml">
    <xsl:copy-of select="."/>
  </xsl:result-document>
</xsl:template>
</xsl:stylesheet>

如果您希望将结果作为JDOM文档列表,那么您可以为Saxon提供一个OutputURIResolver:

Controller controller = transformer.getUnderlyingController();
final Configuration config = controller.getConfiguration();
List<Document> jdomDocuments = new ArrayLis<Document>();
Controller.setOutputURIResolver(new OutputURIResolver() {

   public Result resolve(href, base) {
       return new JDOM2Writer(config.makePipelineConfiguration());
   }

   public void close(Result result) {
       jdomDocuments.add(((JDOM2Writer)result).getDocument());
   }
}

完成后,结果将显示在jdomDocuments