2017-06-01 2 views
1

私たちは、100%データ駆動型のWebSiteを構築しています。可能なすべてのフィールド名はPostgreSQLにあり、これらのフィールドの値はすべてWebサービスからのものです。エンドユーザーは、画面上のフィールドをクリックすることで、独自のページを構築することができます。私は、Webサービスから戻ってくるXMLデータの完全なマッピングをリレーするためのPostgreSQLのテキストフィールドを持つ最善の方法を考え出しています。私はroot \ property1 \ subpropertyを使用して、XMLからそれを分解することで何かループを持つか、より効果的な方法がありますか?PostgreSQL XMLプロパティにマップするフィールド

EDIT:JSONをXMLに置き換えました。最近JSONを使って作業していましたが、これらのREST WebサービスがXMLを返すときにJSONと言いました。

EDIT2:ソリューションの種類が見つかりましたが、下の例からわかるように、ノード名が2回存在する場合、2つのノードとして返されます。 newnode2 \ firstNameとしてDBに保存する必要があるかどうかを調べる必要があります。ノードをループしてnewnode2を探し、firstNameを見つけるためにループする必要があります。私は何年も前に、.NETでXMLオブジェクトを使用していたことを覚えています。@ node @ \ subnodeのようなことをして値を取得できました。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    Document doc = null; 
    try 
    { 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     doc = builder.parse(new InputSource(new StringReader("<response><responseStatus>Success</responseStatus><dataSet name=\"myvalue\"><newnode><firstName>John</firstName><lastName>Smith</lastName></newnode><newnode2><firstName>Bob</firstName><birthDate>11/11/1965</birthDate></newnode2></dataSet></response>"))); 

     NodeList nList = doc.getElementsByTagName("firstName"); 
     Node n; 
     String value; 

     for(int i=0; i<nList.getLength(); i++) 
     { 
      n = nList.item(i); 
      value = n.getTextContent(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

ただ、好奇心:あなたはこのようにMongoDBなど、あらゆるドキュメントベースのNoSQLデータベースを使用しない理由は?それらはあなたが提案している目的のために意図されています。 – russellhoff

+0

Apache Velocityのような動的なテンプレートエンジンを調べることもできます。 – Tezra

+0

@russellhoffビジネス要件..私は、主要なセキュリティが製品を分解し、製品のどれだけのサポートが存在するかの承認を得なければならない金融会社のために働いています。彼らは既にMongoDBでNOを語っています。 – Switch

答えて

0

私は再帰関数を思いついた。あなたは

String xml = "<response><responseStatus>Success</responseStatus><dataSet name=\"myvalue\"><newnode><firstName>John</firstName><lastName>Smith</lastName></newnode><newnode2><firstName>Bob</firstName><birthDate>11/11/1965</birthDate></newnode2></dataSet></response>"; 
String value; 
value = getXMLValue(xml, "newnode2/firstName"); //example of multi-node 
value = getXMLValue(xml, "birthDate");   //example of going directly to field if it's the only node with that name. 
value = getXMLValue(xml, "dataSet/name");  //example of getting attribute 

関数を参照するか、より良い方法を知っている場合は私に知らせてください:

public String getXMLValue(String xml, String searchNodes) { 
    String retVal = ""; 
    String[] nodeSplit = searchNodes.split("/"); 

    try 
    { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     Document doc; 
     NodeList nList; 

     DocumentBuilder builder = factory.newDocumentBuilder(); 
     doc = builder.parse(new InputSource(new StringReader(xml))); 

     nList = doc.getElementsByTagName(nodeSplit[0]); 
     retVal = GetNode(nList, searchNodes, 0); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return retVal; 
} 

public String GetNode(NodeList nl, String searchNodes, int item) 
{ 
    String retVal = null; 
    String findNode = searchNodes.split("/")[item]; 
    int count = searchNodes.split("/").length; 

    item++; 

    for(int i=0; i<nl.getLength(); i++) { 
     String foundNode = nl.item(i).getNodeName(); 
     NamedNodeMap nnm = nl.item(i).getAttributes(); 
     if(nnm!=null && nnm.getLength()>0 && count>item) { 
      Node attribute = nnm.getNamedItem(searchNodes.split("/")[item]); 
      if(attribute!=null) { 
       retVal = attribute.getTextContent(); 
       break; 
      } 
     } 

     if(foundNode.equals(findNode) && count>item) { 
      retVal = GetNode(nl.item(i).getChildNodes(), searchNodes, item); 
      break; 
     } else if(foundNode.equals(findNode) && count==item) { 
      retVal = nl.item(i).getTextContent(); 
      break; 
     } 
    } 

    return retVal; 
} 
関連する問題