如何在Java中转换标签

时间:2015-02-23 13:47:44

标签: java xml soap wsdl

如何在Java中转换下面的代码,我认为使用了XTreams Framework

救救我!我在谷歌搜索中看到了。

:此

<?xml version="1.0" encoding="ISO-8859-1"?> 
<fastbranch-xe-request project="Tech" transaction-id="massiveSelectBranchList_By_Institution_Facade" transaction-version="1.0">
    <entity name="glb.credential">
        <attribute name="channelId">8</attribute>
        <attribute name="originBranchId">0</attribute>
        <attribute name="dependencyId">1000</attribute>
    </entity>
    <entity id="Institution" name="in.institution">
        <attribute name="institutionId">1111</attribute>
    </entity> </fastbranch-xe-request>

对于此

<?xml version="1.0" encoding="ISO-8859-1"?> 
<massiveSelectBranchList_By_Institution_Facade>
    <credential>
        <channelId>8</channelId>
        <originBranchId>0</originBranchId>
        <dependencyId>1000</dependencyId>
    </credential>
    <institution>
        <institutionId>1111</institutionId>
    </institution> 
</massiveSelectBranchList_By_Institution_Facade>

2 个答案:

答案 0 :(得分:1)

我的第二个reineke建议使用DOM来转换初始请求。这是解析初始请求的先机:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("xml");

// optional, but recommended
// read this -
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();

// System.out.println("Root element :" +
// doc.getDocumentElement().getNodeName());

// This breaks the document apart by the specified tag
NodeList nList = doc.getElementsByTagName("fastbranch-xe-request");

// For each instance of the "fastbranch-xe-request" tag...
for (int pos = 0; pos < nList.getLength(); pos++) {
    Node nNode = nList.item(pos);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;

        // This gets you: "massiveSelectBranchList_By_Institution_Facade"
        String transactionID = eElement.getAttribute("transaction-id");

        // This gets you: "glb.credential"
        String entityName = eElement.getElementsByTagName("entity").item(0).
                getAttributes().getNamedItem("name").toString();

        // Continue this until you've pulled the tag names from each
        // "attribute" tag.  Once you've pulled all the names (and parsed
        // the values as well), you can use DOM to construct the second
        // XML request.
    }
}

答案 1 :(得分:0)

我不知道XTreams框架。在Java中,通常选择SAX或DOM进行XML解析。

一个简单的例子: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

或一个好的教程:http://howtodoinjava.com/2014/07/31/java-xml-dom-parser-example-tutorial/

只是为了让你朝着正确的方向前进。如果您在应用本教程时遇到困难,请展示您目前所获得的内容,我很乐意为您提供帮助。

如果你没有绑定java,你可以考虑使用java的XSLT instad。它特别适合这种任务,而且更短更容易。

希望我能提供帮助 reineke