I download an XML-file, I generate using PHP, that looks similar to this
<?xml version="1.0" encoding="utf-8" ?>
<customersXML>
...
<customer id="12" name="Me+%26+My+Brother" swid="1" />
...
</customersXML>
Now I need to parse it in Java, but before that I use URL-Decode, so the XML become this
<?xml version="1.0" encoding="utf-8" ?>
<customersXML>
...
<customer id="12" name="Me & My Brother" swid="1" />
...
</customersXML>
But when I parse the XML-file using SAX, I get a problem with "&". How can I get around this?
The ampersand is a special character in xml (O'reilly Xml: Entities: Handling Special Content) and needs to be encoded. Replace it with & before sending it.
If the XML in question isn't urlencoded in the first place (which it doesn't look like it is), then you shouldn't be urldecoding it. Breaking the xml and then "unbreaking" it really doesn't seem like the best way to go about it. Just use the original xml and parse that.
Never process XML as a string without parsing it, or you are liable to end up with something that is no longer XML. As you have discovered.
You should FIRST parse, THEN url decode.
Related
I am trying to read this XML file using PHP and I have two root elements. The code that I wrote in PHP reads only one root element and when I add the other one (<action>) it gives me an error.
I want to do something like this : if($xml->action=="register") then print all parameters.
This is my XML file:
<?xml version='1.0' encoding='ISO-8859-1'?>
<action>register</action>
<paramters>
<name>Johnny B</name>
<username>John</username>
</paramters>
And this is my PHP script:
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
I really don't know how to do all this...
Fix your XML, it's invalid. XML files can only have 1 root element.
Example valid XML:
<?xml version='1.0' encoding='ISO-8859-1'?>
<action>
<type>register</type>
<name>Johnny B</name>
<username>John</username>
</actions>
Or if you want only parameters to have own elements:
<?xml version='1.0' encoding='ISO-8859-1'?>
<action type="register">
<name>Johnny B</name>
<username>John</username>
</actions>
or if you want multiple actions:
<?xml version='1.0' encoding='ISO-8859-1'?>
<actions>
<action type="register">
<name>Johnny B</name>
<username>John</username>
</action>
</actions>
EDIT:
As I've said in my comment, your teacher should fix his XML. It is invalid. Also he should put his XML through a validator.
If you're really desperate you can introduce an articificial root element, but this is really bad practice and should be avoided at all costs:
$xmlstring = str_replace(
array('<action>','</paramters>'),
array('<root><action>', '</paramters></root>'),
$xmlstring
);
None of the previous answers is quite accurate. The XML specification defines several kinds of entity: document entities, external parsed entities, document type definitions for example. Your example is not a well-formed document entity, which is what XML parsers are normally asked to parse. However, it is a well-formed external parsed entity. The way to process a well-formed external parsed entity is to reference it from a skeletal document entity, like this:
<!DOCTYPE wrapper [
<!ENTITY e SYSTEM "my.xml">
]>
<wrapper>&e;</wrapper>
and then pass the document entity to the XML parser.
As it is an invalid xml file, you can do the following trick.
Insert a dummy start tag at the second line as <dummy>
In the end finish it with </dummy>
Happy parsing ;)
It's a really simple question but which I have no quick answer to it and I need help : I call a service that returns this XML body and need to parse it and get the element's values but for some reason I always get the values as null all the time.
How can I parse this XML body via any recommended method in java ?
<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns="http://tempuri.org/Response.xsd">
<ResponseStatusDescription />
<EntityPaymentReceiptNumber />
<Description>Test</Description>
<OperationName>CheckPayment</OperationName>
<BankID>39</BankID>
<EntityPaymentDate />
<CheckPaymentID>188721103486</CheckPaymentID>
<ResponseStatusCode>INFO2</ResponseStatusCode>
</Response>
Generate a class from the xsd, i.e xjc http://tempuri.org/Response.xsd. Now, have your rest call expect Response as the return type.
I am using below format to response for the webservices.
<Name>abc</Name>
<Detail>
<RESPONSE>
<Age>20</Age>
<Address>blahblah</Address>
<Mobile>12345</Mobile>
</RESPONSE>
</Detail>
Due to the requirements, I need to return xml format data insides the <Detail></Detail> tag.
In my java class, I parse using Xstream and format into xml and put insides the Detail tag.
But when I test using SOAPUI , I am getting extra <![CDATA[<RESPONSE>.. <</RESPONSE>]]> insdies Detail tag.
How can I avoid having those CDATA tag for the xml response?
<![CDATAP[......]]> is used to tell that the XML meaning of it should not be taken and to treat it as normal text that is called character data. so Parser won't seek for any XML meaning in it.
As Dave Newton and kshitij told it will automatically removed while converting it into object.
If you are not supposed to parse it as it is no issue to bother about it.
i am working on project , in that there is one xml file (IDE Eclipse Indigo).
I am facing a problem with sincle line
<?xml version="1.0" encoding="UTF-8"?>
<BookingConfirmRQ xmlns="http://www.expediaconnect.com/EQC/BC/2007/09"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Authentication username="yyyyyyyy" password="xxxxxxxx" />
<Hotel id="<hotelId/>" />
<BookingConfirmNumbers>
<BookingConfirmNumber bookingID="<bookindId/>"
bookingType="<bookingType/>" confirmNumber="<confirmNumber/>"
confirmTime="<confirmTime/>" />
</BookingConfirmNumbers>
</BookingConfirmRQ>
Here near < Hotel id="< hotelId/>"/> am getting error like_
The value of attribute "id" associated with an element type "Hotel" must not contain the '<' character.
i search it , checked jar's, reformatted still getting error, can sombody help me?
thank u.
You can ignore validation of XML from eclipse windows-preference-validation menu and this way if you don't want to change you can avoid this error
Attribute values should only contain literal text:
<Hotel id="134" />
You need to escape the angle brackets in the value of the attribute like this:
<Hotel id="<hotelId/>" />
Same with the all the other attributes. The angle brackets are on the list of reserved characters that have to be escaped in XML.
Unless you do that, the XML is not well-formed and nothing will process it. Turning off validation - i.e. validation against a DTD or schema - will not help here. The XML has to be well-formed before it can be parsed.
That said, the XML looks very odd, as if you're including whole XML-elements as the value of attributes which is just wrong. So even if you fix the escaping problem this XML may not say what you meant.
I am trying to generate xml using doxygen from java sourcecode. Doxygen doesn't parse tags like
<code>,<value> and \s\p.... correctly. It generates xml with incorrect values.
For example:
<code>0x0</code> tag is converted into <computeroutput>0x0</computeroutput>.
<para>
<computeroutput>This is code tag</computeroutput>
<value2>test value4</value2> </meta> </meta> <gid>000001</gid> <read>1</read>
</parameter> </component> </algebra>
</para>
similarly for other tags like <value> and \s\p also.
I am wondering why it happens?????
Please let me know what are all other tags also will produce the same output
and how to resolve it.
"correctly" is a bit of a misnomer when referring to xml, unless it weren't structured correctly, but I think you're referring to the tags.
If you don't like the output from doxygen why not write an xslt to make it whatever you want? I'm sure there are many doxygen.xml --> myflavor.xml transforms out there that you could use as a starting point.