2017-04-27 3 views
0

ich ein xml/mathml Fragment in Zeichenfolge habe wie <msup><mn>2</mn><mn>6</mn></msup><mo>+</mo><msup><mn>2</mn><mn>7</mn></msup>hinzufügen XML String als Knoten zu vorhandenen XML

ich einen vorhandenen XML-Knoten mit dieser Zeichenfolge ersetzt werden soll. Ich

versucht
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuild = docFactory.newDocumentBuilder(); 
    Document doc = docBuild.parse(is); //is ==> Inputstream 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    xpath.setNamespaceContext(new Namespace()); 
    XPathExpression expr = xpath.compile(xPath); //xPath ==>XPath of parent node where the above is to be appended 
    Node node = (Node) expr.evaluate(doc, XPathConstants.NODE); 
    if (node.getChildNodes().item(0).getNodeName().equals("mml:math")) { 
     node.removeChild((node.getChildNodes().item(0))); 
     Element s= doc.createElement("mml:math"); 
     s.setNodeValue("<msup><mn>2</mn><mn>6</mn></msup><mo>+</mo><msup><mn>2</mn><mn>7</mn></msup>"); 
     node.appendChild(s); 
    } 
+0

Was ist mit dem Code passiert, den Sie ausprobiert haben? –

+0

XML wurde nicht aktualisiert .... –

+0

Wenn Sie Quelle xml, wäre es einfach, Ihre Frage zu beantworten – ssanrao

Antwort

0

Statt s.setNodeValue() verwenden, benutzte ich s.setTextContent(), die mein Problem behoben.

Verwandte Themen