简体   繁体   中英

How to transform tags in Java

How to transform tags below in Java , I thinked use the XTreams Framework .

Help me! I see in Google search.

This

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

For this

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

I second reineke's suggestion of using DOM to transform the initial request. Here's a head start for you on parsing the initial request:

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

I don't know about the XTreams Framework. In Java conventionally SAX or DOM is chosen for XML parsing.

a quick example: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

or a good tutorial: http://howtodoinjava.com/2014/07/31/java-xml-dom-parser-example-tutorial/

Just to put you in the right direction. If you have difficulties with applying the tutorial, show what you got so far, and Im glad to help.

If you are NOT bound to java, you might consider using XSLT instad of java. its especially made for this kind of tasks, and shorter and easier.

Hope I could help reineke

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM