0

I am newbie in XML parsing world. I found solution for reading xml file with 'namespaces' as given below. I refered this stackoverflow link Default XML namespace, JDOM, and XPath and reading Element works. But I am not able to modify the element. Here goes my problem statements.

My xml file looks like this.

<?xml version="1.0" encoding="UTF-8"?>
<ProofSpecification xmlns="http://www.zurich.ibm.com/security/idemix"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.zurich.ibm.com/security/idemix ProofSpecification.xsd">

    <Declaration>
        <AttributeId name="id1" proofMode="unrevealed" type="string" />
        <AttributeId name="id2" proofMode="unrevealed" type="string" />
    </Declaration>

    <Specification>
        <Credentials>
            <Credential issuerPublicKey="file:/Users/ipk.xml"
            credStruct="file:/Users/CredStruct_ResUAC.xml" name="SpecResUAC">
                <Attribute name="FirstName">id1</Attribute>
                <Attribute name="LastName">id2</Attribute>
            </Credential>
        </Credentials>

        <Pseudonyms>
            <Pseudonym name="pseudonym"></Pseudonym>
            <DomainPseudonym>1331859289489</DomainPseudonym>
        </Pseudonyms>

            <Messages />
    </Specification>
</ProofSpecification>

And my java code snippet looks like this.

public class ModifyXMLFileJDom {

    public static void main(String[] args) throws JDOMException, IOException {

      try {
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("/blabla/ProofSpecResUAC_old.xml");

        Document doc = (Document) builder.build(xmlFile); 

        XPath xpath = XPath.newInstance("x:ProofSpecification/x:Specification/x:Pseudonyms/x:DomainPseudonym");
        xpath.addNamespace("x", doc.getRootElement().getNamespaceURI());
        System.out.println("domainPseudonym: "+xpath.valueOf(doc));
        xpath.setVariable("555555", doc);

        XMLOutputter xmlOutput = new XMLOutputter();

        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter("/blabla/proofSpecOut.xml"));

        System.out.println("File updated!");
      } catch (IOException io) {
        io.printStackTrace();
      } catch (JDOMException e) {
        e.printStackTrace();
      }
    }
}

This piece of code works fine and Reading Element "domainPseudonym" works.

But I want to modify this element from 
 <DomainPseudonym>1331859289489</DomainPseudonym> to
 <DomainPseudonym>555555</DomainPseudonym>

I tried modifying using function xpath.setVariable("555555", doc) but not working and not giving out any error. The end result is that it copies the same content to the new xml file "proofSpecOut.xml".

1 Answer 1

2

XPath is not used for modifying the xml, only for finding parts of it. XPath.setVariable() is only for setting variables in your xpath expression. You want to use XPath.selectSingleNode() to retrieve one of the Elements of your document and then you want to modify that directly using Element.setText().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot jthalborn!! It worked! I was struggling this since many morning trying out different parsers and other examples! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.