2016-11-15 96 views
2

我有以下XML字符串。使用JDOM Java拆分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是給出工程師/ 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> 

我已經實現了使用撒克遜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 

如何更改代碼,使我得到我想要的輸出?還是有辦法讓名字的子節點屬性(姓名,職務,隊)的內部工程師

回答

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

我會做在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文檔的列表,那麼你可以用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